home *** CD-ROM | disk | FTP | other *** search
/ Quick PC 61 / Quick PC 61.iso / I386 / SQLRUN.CAB / replcom.sql.24170C3F_A9E4_47C1_8DFE_FD79C2714697 < prev    next >
Encoding:
Text File  |  2002-12-11  |  439.0 KB  |  13,461 lines

  1. /*
  2. ** replcom.sql            1997/02/12 22:03
  3. **
  4. **
  5. ** Copyright Microsoft, Inc. 1998-2000
  6. ** All Rights Reserved.
  7. */
  8.  
  9.  
  10. dump tran master with no_log
  11. go
  12.  
  13. exec dbo.sp_configure 'update',1
  14. go
  15. reconfigure with override
  16. go
  17.  
  18. set ANSI_NULLS off
  19. go
  20.  
  21. use master
  22. go
  23.  
  24. exec dbo.sp_MS_upd_sysobj_category 1 --Capture time for use at the end
  25. go
  26.  
  27.  
  28. dump tran master with no_log
  29. GO
  30.  
  31. /* 
  32. ** Drop the stored procedures in this script using the old dropping SP 
  33. ** and then drop itself
  34. */
  35. if exists (select * from sysobjects
  36.     where type = 'P'
  37.             and name = 'sp_MSdrop_replcom')
  38. begin
  39.     drop procedure sp_MSdrop_replcom
  40. end
  41.  
  42. /*
  43. ** Create stored procedures to drop the stored procedures
  44. ** created by this script
  45. */
  46.  
  47. raiserror('Creating procedure sp_MSdrop_replcom', 0,1)
  48. GO
  49.  
  50. create procedure sp_MSdrop_replcom
  51. as
  52.     if exists (select * from sysobjects 
  53.         where type = 'P'
  54.             and name = 'sp_MScreate_distributor_tables')
  55.         drop procedure sp_MScreate_distributor_tables
  56.  
  57.     if exists (select * from sysobjects
  58.         where type = 'P'
  59.                 and name = 'sp_MSIfExistsRemoteLogin')
  60.         drop procedure sp_MSIfExistsRemoteLogin
  61.  
  62.     if exists (select * from sysobjects
  63.         where type = 'P'
  64.                 and name = 'sp_helppublicationsync')
  65.         drop procedure sp_helppublicationsync
  66.  
  67.     if exists (select * from sysobjects
  68.             where type = 'P'
  69.                 and name = 'sp_addpublication_snapshot')
  70.         drop procedure sp_addpublication_snapshot
  71.  
  72.     if exists (select * from sysobjects
  73.             where type = 'P'
  74.                 and name = 'sp_MShelpobjectpublications')
  75.         drop procedure sp_MShelpobjectpublications
  76.  
  77.     if exists (select * from sysobjects
  78.             where type = 'P'
  79.                 and name = 'sp_addpublisher')
  80.         drop procedure sp_addpublisher
  81.  
  82.     if exists (select * from sysobjects
  83.             where type = 'P'
  84.                 and name = 'sp_addsubscriber')
  85.         drop procedure sp_addsubscriber
  86.  
  87.     if exists (select * from sysobjects
  88.             where type = 'P'
  89.                 and name = 'sp_addsubscriber_schedule')
  90.         drop procedure sp_addsubscriber_schedule
  91.  
  92.     if exists (select * from sysobjects
  93.             where type = 'P'
  94.                 and name = 'sp_changesubscriber_schedule')
  95.         drop procedure sp_changesubscriber_schedule
  96.  
  97.     if exists (select * from sysobjects
  98.             where type = 'P'
  99.                 and name = 'sp_changesubscriber')
  100.         drop procedure sp_changesubscriber
  101.  
  102.     IF EXISTS (SELECT * FROM sysobjects
  103.             WHERE type = 'P'
  104.                 AND name = 'sp_MScreate_dist_tables')
  105.         DROP PROCEDURE sp_MScreate_dist_tables
  106.  
  107.     IF EXISTS (SELECT * FROM sysobjects
  108.             WHERE type = 'P'
  109.                 AND name = 'sp_MSupdate_mqserver_distdb')
  110.         DROP PROCEDURE sp_MSupdate_mqserver_distdb
  111.  
  112.     if exists (select * from sysobjects
  113.             where type = 'P'
  114.                 and name = 'sp_distcounters')
  115.         drop procedure sp_distcounters
  116.  
  117.  
  118.     if exists (select * from sysobjects
  119.             where type = 'P'
  120.                 and name = 'sp_droppublisher')
  121.         drop procedure sp_droppublisher
  122.  
  123.     if exists (select * from sysobjects
  124.             where type = 'P'
  125.                 and name = 'sp_dropsubscriber')
  126.         drop procedure sp_dropsubscriber
  127.  
  128.     if exists (select * from sysobjects
  129.             where type = 'P'
  130.                 and name = 'sp_dsninfo')
  131.         drop procedure sp_dsninfo
  132.  
  133.     if exists (select * from sysobjects
  134.             where type = 'P'
  135.                 and name = 'sp_enumdsn')
  136.         drop procedure sp_enumdsn
  137.  
  138.     if exists (select * from sysobjects
  139.             where type = 'P'
  140.                 and name = 'sp_helpdistributor')
  141.         drop procedure sp_helpdistributor
  142.  
  143.     if exists (select * from sysobjects
  144.             where type = 'P'
  145.                 and name = 'sp_helppublicationsync')    /* Remove old version */
  146.         drop procedure sp_helppublicationsync
  147.  
  148.     if exists (select * from sysobjects
  149.             where type = 'P'
  150.                 and name = 'sp_helpreplicationdb')
  151.         drop procedure sp_helpreplicationdb
  152.  
  153.     if exists (select * from sysobjects
  154.             where type = 'P'
  155.                 and name = 'sp_helpsubscriberinfo')
  156.         drop procedure sp_helpsubscriberinfo
  157.  
  158.     if exists (select * from sysobjects
  159.             where type = 'P'
  160.                 and name = 'sp_publishdb')
  161.         drop procedure sp_publishdb
  162.  
  163.     if exists (select * from sysobjects
  164.             where type = 'P'
  165.                 and name = 'sp_replica')
  166.         drop procedure sp_replica
  167.  
  168.     if exists (select * from sysobjects
  169.             where type = 'P'
  170.                 and name = 'sp_adddistributiondb')
  171.         drop procedure sp_adddistributiondb
  172.  
  173.     if exists (select * from sysobjects
  174.             where type = 'P'
  175.                 and name = 'sp_changedistributiondb')
  176.         drop procedure sp_changedistributiondb
  177.  
  178.     if exists (select * from sysobjects
  179.             where type = 'P'
  180.                 and name = 'sp_helpdistributiondb')
  181.         drop procedure sp_helpdistributiondb
  182.  
  183.     if exists (select * from sysobjects
  184.             where type = 'P'
  185.                 and name = 'sp_dropdistributiondb')
  186.         drop procedure sp_dropdistributiondb
  187.  
  188.     if exists (select * from sysobjects
  189.             where type = 'P'
  190.                 and name = 'sp_adddistpublisher')
  191.         drop procedure sp_adddistpublisher
  192.  
  193.     if exists (select * from sysobjects
  194.             where type = 'P'
  195.                 and name = 'sp_changedistpublisher')
  196.         drop procedure sp_changedistpublisher
  197.  
  198.     if exists (select * from sysobjects
  199.             where type = 'P'
  200.                 and name = 'sp_helpdistpublisher')
  201.         drop procedure sp_helpdistpublisher
  202.  
  203.     if exists (select * from sysobjects
  204.             where type = 'P'
  205.                 and name = 'sp_dropdistpublisher')
  206.         drop procedure sp_dropdistpublisher
  207.  
  208.     if exists (select * from sysobjects
  209.             where type = 'P'
  210.                 and name = 'sp_MSadd_distributor_alerts_and_responses')
  211.         drop procedure  sp_MSadd_distributor_alerts_and_responses
  212.  
  213.     if exists (select * from sysobjects
  214.             where type = 'P'
  215.                 and name = 'sp_MSdrop_distributor_alerts_and_responses')
  216.         drop procedure  sp_MSdrop_distributor_alerts_and_responses
  217.  
  218.     if exists (select * from sysobjects
  219.             where type = 'P'
  220.                 and name = 'sp_adddistributor')
  221.         drop procedure  sp_adddistributor
  222.  
  223.     if exists (select * from sysobjects
  224.             where type = 'P'
  225.                 and name = 'sp_dropdistributor')
  226.         drop procedure sp_dropdistributor
  227.  
  228.     if exists (select * from sysobjects
  229.             where type = 'P'
  230.                 and name = 'sp_changedistributor_property')
  231.         drop procedure sp_changedistributor_property
  232.  
  233.     if exists (select * from sysobjects
  234.             where type = 'P'
  235.                 and name = 'sp_helpdistributor_properties')
  236.         drop procedure sp_helpdistributor_properties
  237.  
  238.     if exists (select * from sysobjects
  239.             where type = 'X'
  240.                 and name = 'sp_repldone')
  241.         exec dbo.sp_dropextendedproc 'sp_repldone'
  242.  
  243.     if exists (select * from sysobjects
  244.             where type = 'X'
  245.                 and name = 'sp_repltrans')
  246.         exec dbo.sp_dropextendedproc 'sp_repltrans'
  247.  
  248.     if exists (select * from sysobjects
  249.             where type = 'X'
  250.                 and name = 'sp_replcmds')
  251.         exec dbo.sp_dropextendedproc 'sp_replcmds'
  252.  
  253.     if exists (select * from sysobjects
  254.             where type = 'X'
  255.                 and name = 'sp_replcounters')
  256.         exec dbo.sp_dropextendedproc 'sp_replcounters'
  257.  
  258.     if exists (select * from sysobjects
  259.             where type = 'X'
  260.                 and name = 'sp_replflush')
  261.         exec dbo.sp_dropextendedproc 'sp_replflush'
  262.  
  263.     if exists (select * from sysobjects
  264.             where type = 'X'
  265.                  and name = 'sp_replpostcmd' )
  266.         exec dbo.sp_dropextendedproc 'sp_replpostcmd'
  267.  
  268.     if exists (select * from sysobjects
  269.             where type = 'X'
  270.                  and name = 'sp_replpostschema' )
  271.         exec dbo.sp_dropextendedproc 'sp_replpostschema'
  272.  
  273.     if exists (select * from sysobjects
  274.             where type = 'X'
  275.                  and name = 'sp_replincrementlsn' )
  276.         exec dbo.sp_dropextendedproc 'sp_replincrementlsn'
  277.  
  278.     if exists (select * from sysobjects
  279.             where type = 'P'
  280.                  and name = 'sp_replincrementlsn' )
  281.         drop procedure sp_replincrementlsn
  282.  
  283.     if exists (select * from sysobjects
  284.             where type = 'X'
  285.                  and name = 'sp_replincrementlsn_internal' )
  286.         exec dbo.sp_dropextendedproc 'sp_replincrementlsn_internal'
  287.  
  288.     if exists (select * from sysobjects
  289.             where type = 'X'
  290.                  and name = 'sp_replupdateschema' )
  291.         exec dbo.sp_dropextendedproc 'sp_replupdateschema'
  292.  
  293.     if exists (select * from sysobjects
  294.             where type = 'X'
  295.                  and name = 'sp_replsetoriginator_internal' )
  296.         exec dbo.sp_dropextendedproc 'sp_replsetoriginator_internal'
  297.  
  298.     if exists (select * from sysobjects
  299.             where type = 'X'
  300.                  and name = 'sp_replsetoriginator' )
  301.         exec dbo.sp_dropextendedproc 'sp_replsetoriginator'
  302.  
  303.     if exists (select * from sysobjects
  304.             where type = 'P'
  305.                  and name = 'sp_replsetoriginator' )
  306.         drop procedure sp_replsetoriginator
  307.  
  308.     if exists (select * from sysobjects
  309.             where type = 'P'
  310.                  and name = 'sp_replsetoriginator_pal' )
  311.         drop procedure sp_replsetoriginator_pal
  312.  
  313.     if exists (select * from sysobjects
  314.             where type = 'X'
  315.                  and name = 'sp_replsetsyncstatus' )
  316.         exec dbo.sp_dropextendedproc 'sp_replsetsyncstatus'
  317.  
  318.     if exists (select * from sysobjects
  319.             where type = 'X'
  320.                  and name = 'sp_replpostsyncstatus' )
  321.         exec dbo.sp_dropextendedproc 'sp_replpostsyncstatus'
  322.  
  323.     if exists (select * from sysobjects
  324.             where type = 'P'
  325.                  and name = 'sp_replpostsyncstatus' )
  326.         drop procedure sp_replpostsyncstatus
  327.  
  328.     if exists (select * from sysobjects
  329.             where type = 'X'
  330.                  and name = 'sp_replpostsyncstatus_int' )
  331.         exec dbo.sp_dropextendedproc 'sp_replpostsyncstatus_int'
  332.  
  333.     if exists (select * from sysobjects
  334.             where type = 'X'
  335.                 and name = 'xp_enumdsn')
  336.         exec dbo.sp_dropextendedproc 'xp_enumdsn'
  337.  
  338.     if exists (select * from sysobjects
  339.             where type = 'X'
  340.                 and name = 'xp_oledbinfo')
  341.         exec dbo.sp_dropextendedproc 'xp_oledbinfo'
  342.  
  343.     if exists (select * from sysobjects
  344.             where type = 'X'
  345.                 and name = 'xp_dsninfo')
  346.         exec dbo.sp_dropextendedproc 'xp_dsninfo'
  347.  
  348.     if exists (select * from sysobjects
  349.             where type = 'X'
  350.                 and name = 'xp_repl_encrypt')
  351.         exec dbo.sp_dropextendedproc 'xp_repl_encrypt'
  352.  
  353.     if exists (select * from sysobjects
  354.             where type = 'X'
  355.                 and name = 'xp_repl_convert_encrypt')
  356.         exec dbo.sp_dropextendedproc 'xp_repl_convert_encrypt'
  357.  
  358.     if exists (select * from sysobjects
  359.             where type = 'X'
  360.                 and name = 'xp_repl_help_connect')
  361.         exec dbo.sp_dropextendedproc 'xp_repl_help_connect'
  362.  
  363.      if exists (select * from sysobjects
  364.             where type = 'X'
  365.                 and name = 'xp_replproberemsrv')
  366.         exec dbo.sp_dropextendedproc 'xp_replproberemsrv'
  367.  
  368.     -- sp_helpsubscriber is removed permanently from the system.
  369.     if exists (select * from sysobjects
  370.         where type = 'P'
  371.                 and name = 'sp_helpsubscriber')
  372.         drop procedure sp_helpsubscriber
  373.  
  374.     -- sp_MSrepl_encrypt obsolete; use xp_repl_encrypt
  375.     if exists (select * from sysobjects
  376.         where type = 'P'
  377.                 and name = 'sp_MSrepl_encrypt')
  378.         drop procedure sp_MSrepl_encrypt
  379.  
  380.     if exists (select * from sysobjects 
  381.         where name = 'sp_add_agent_profile' 
  382.                 and type = 'P')
  383.         drop procedure sp_add_agent_profile
  384.  
  385.     if exists (select * from sysobjects 
  386.         where name = 'sp_help_agent_profile' 
  387.                 and type = 'P')
  388.         drop procedure sp_help_agent_profile
  389.  
  390.     if exists (select * from sysobjects 
  391.         where name = 'sp_help_agent_default' 
  392.             and type = 'P')
  393.         drop procedure sp_help_agent_default
  394.  
  395.     if exists (select * from sysobjects 
  396.         where name = 'sp_drop_agent_profile' 
  397.                 and type = 'P')
  398.         drop procedure sp_drop_agent_profile
  399.  
  400.     if exists (select name from sysobjects 
  401.         where name = 'sp_MSupdate_agenttype_default'
  402.                 and type = 'P')
  403.         drop procedure sp_MSupdate_agenttype_default
  404.  
  405.     if exists (select * from sysobjects 
  406.         where name = 'sp_MSvalidate_agent_parameter' 
  407.                 and type = 'P')
  408.         drop procedure sp_MSvalidate_agent_parameter
  409.  
  410.     if exists (select * from sysobjects 
  411.         where name = 'sp_add_agent_parameter' 
  412.                 and type = 'P')
  413.         drop procedure sp_add_agent_parameter
  414.  
  415.     if exists (select * from sysobjects 
  416.         where name = 'sp_generate_agent_parameter' 
  417.                 and type = 'P')
  418.         drop procedure sp_generate_agent_parameter
  419.  
  420.     if exists (select * from sysobjects 
  421.         where name = 'sp_change_agent_parameter' 
  422.                 and type = 'P')
  423.         drop procedure sp_change_agent_parameter
  424.  
  425.     if exists (select * from sysobjects 
  426.         where name = 'sp_change_agent_profile' 
  427.                 and type = 'P')
  428.         drop procedure sp_change_agent_profile
  429.  
  430.     if exists (select * from sysobjects 
  431.         where name = 'sp_help_agent_parameter' 
  432.                 and type = 'P')
  433.       drop procedure sp_help_agent_parameter
  434.  
  435.     if exists (select * from sysobjects 
  436.         where name = 'sp_drop_agent_parameter' 
  437.                 and type = 'P')
  438.       drop procedure sp_drop_agent_parameter
  439.  
  440.     if exists (select * from sysobjects 
  441.         where name = 'sp_MShelp_distdb' 
  442.                 and type = 'P')
  443.       drop procedure sp_MShelp_distdb
  444.  
  445.     if exists (select * from sysobjects 
  446.         where name = 'sp_MShelp_distdb' 
  447.                 and type = 'P')
  448.       drop procedure sp_MShelp_distdb
  449.  
  450.     if exists (select * from sysobjects 
  451.         where name = 'sp_MSenum_misc_agents'
  452.                 and type = 'P')
  453.       drop procedure sp_MSenum_misc_agents
  454.  
  455.     if exists (select * from sysobjects 
  456.         where name = 'sp_MSupdate_replication_status' 
  457.                 and type = 'P')
  458.       drop procedure sp_MSupdate_replication_status
  459.  
  460.     if exists (select * from sysobjects 
  461.         where name = 'sp_MSload_replication_status' 
  462.                 and type = 'P')
  463.       drop procedure sp_MSload_replication_status
  464.  
  465.     if exists (select * from sysobjects 
  466.         where name = 'sp_MScreate_replication_status_table' 
  467.                 and type = 'P')
  468.       drop procedure sp_MScreate_replication_status_table
  469.  
  470.     if exists (select * from sysobjects 
  471.         where name = 'sp_MShelp_replication_status' 
  472.                 and type = 'P')
  473.       drop procedure sp_MShelp_replication_status
  474.  
  475.     if exists (select * from sysobjects 
  476.         where name = 'sp_MSenum_replication_agents' 
  477.                 and type = 'P')
  478.       drop procedure sp_MSenum_replication_agents
  479.  
  480.     if exists (select * from sysobjects 
  481.         where name = 'sp_replication_agent_checkup' 
  482.                 and type = 'P')
  483.       drop procedure sp_replication_agent_checkup
  484.  
  485.     if exists (select * from sysobjects 
  486.         where name = 'sp_MSreplrole' 
  487.                 and type = 'P')
  488.       drop procedure sp_MSreplrole
  489.  
  490.     if exists (select * from sysobjects 
  491.         where name = 'sp_MScreate_replication_checkup_agent' 
  492.                 and type = 'P')
  493.       drop procedure sp_MScreate_replication_checkup_agent
  494.  
  495.     if exists (select * from sysobjects 
  496.         where name = 'sp_MSenum_replication_job' 
  497.                 and type = 'P')
  498.       drop procedure sp_MSenum_replication_job
  499.  
  500.     if exists (select * from sysobjects 
  501.         where name = 'sp_MSrepl_dbrole' 
  502.                 and type = 'P')
  503.       drop procedure sp_MSrepl_dbrole
  504.  
  505.     if exists (select * from sysobjects
  506.         where type = 'P'
  507.                 and name = 'sp_oledbinfo')
  508.         drop procedure sp_oledbinfo
  509.  
  510.     if exists (select * from sysobjects
  511.         where type = 'P'
  512.                 and name = 'sp_enumoledbdatasources')
  513.         drop procedure sp_enumoledbdatasources
  514.  
  515.     if exists (select * from sysobjects
  516.         where type = 'P'
  517.                 and name = 'sp_MSget_oledbinfo')
  518.         drop procedure sp_MSget_oledbinfo
  519.  
  520.     if exists (select * from sysobjects 
  521.         where name = 'sp_changedistributor_password' 
  522.                 and type = 'P')
  523.       drop procedure sp_changedistributor_password
  524.  
  525.     if exists (select * from sysobjects 
  526.         where name = 'sp_grant_publication_access' 
  527.                 and type = 'P')
  528.       drop procedure sp_grant_publication_access
  529.  
  530.     if exists (select * from sysobjects 
  531.         where name = 'sp_revoke_publication_access' 
  532.                 and type = 'P')
  533.       drop procedure sp_revoke_publication_access
  534.  
  535.     if exists (select * from sysobjects 
  536.         where name = 'sp_help_publication_access' 
  537.                 and type = 'P')
  538.       drop procedure sp_help_publication_access
  539.  
  540.     if exists (select * from sysobjects 
  541.         where name = 'sp_check_publication_access' 
  542.                 and type = 'P')
  543.       drop procedure sp_check_publication_access
  544.  
  545.     if exists (select * from sysobjects 
  546.         where name = 'sp_MSinit_replication_perfmon' 
  547.                 and type = 'P')
  548.       drop procedure sp_MSinit_replication_perfmon
  549.  
  550.     if exists (select * from sysobjects 
  551.         where name = 'sp_MSrepl_startup' 
  552.                 and type = 'P')
  553.       drop procedure sp_MSrepl_startup
  554.  
  555.     if exists (select * from sysobjects 
  556.         where name = 'sp_MSflush_access_cache' 
  557.                 and type = 'P')
  558.       drop procedure sp_MSflush_access_cache
  559.  
  560.     if exists (select * from sysobjects 
  561.         where name = 'sp_MSreinit_failed_subscriptions' 
  562.                 and type = 'P')
  563.       drop procedure sp_MSreinit_failed_subscriptions
  564.  
  565.     if exists (select * from sysobjects 
  566.         where name = 'sp_add_datatype_mapping' 
  567.                 and type = 'P')
  568.         drop procedure sp_add_datatype_mapping
  569.     
  570.     if exists (select * from sysobjects 
  571.         where name = 'sp_help_datatype_mapping' 
  572.                 and type = 'P')
  573.         drop procedure sp_help_datatype_mapping
  574.  
  575.     if exists (select * from sysobjects 
  576.         where name = 'sp_MSrepl_gettype_mappings' 
  577.                 and type = 'P')
  578.         drop procedure sp_MSrepl_gettype_mappings
  579.         
  580.     if exists (select * from sysobjects 
  581.         where name = 'sp_MSfix_6x_tasks' 
  582.                 and type = 'P')
  583.         drop procedure sp_MSfix_6x_tasks
  584.  
  585.     if exists (select * from sysobjects 
  586.         where name = 'sp_MSget_agent_names' 
  587.                 and type = 'P')
  588.         drop procedure sp_MSget_agent_names
  589.  
  590.     -- Common conflict viewer support procs
  591.     if exists (select * from sysobjects
  592.             where type = 'P'
  593.                 and name = 'sp_MShelpconflictpublications')
  594.         drop procedure sp_MShelpconflictpublications
  595.  
  596.     -- Procs for managing dynamic snapshot views
  597.     if exists (select * from sysobjects
  598.             where type = 'P'
  599.                 and name = 'sp_MScleanupdynsnapshotvws')
  600.         drop procedure sp_MScleanupdynsnapshotvws
  601.  
  602.     if exists (select * from sysobjects
  603.             where type = 'P'
  604.                 and name = 'sp_MScleanupmergepublisher')
  605.         drop procedure sp_MScleanupmergepublisher
  606.  
  607.     if exists (select * from sysobjects
  608.             where type = 'P'
  609.                 and name = 'sp_MScleanupmergepublisherdb')
  610.         drop procedure sp_MScleanupmergepublisherdb
  611.     
  612.     if exists (select * from sysobjects
  613.             where type = 'P'
  614.                 and name = 'sp_MShelp_replication_table')
  615.         drop procedure sp_MShelp_replication_table
  616.     if exists (select * from sysobjects
  617.             where type = 'P'
  618.                 and name = 'sp_MScopyscriptfile')
  619.         drop procedure sp_MScopyscriptfile
  620.  
  621.     if exists (select * from sysobjects
  622.             where type = 'FN'
  623.                 and name = 'fn_replgetagentcommandlinefromjobid')
  624.         drop function system_function_schema.fn_replgetagentcommandlinefromjobid
  625.     
  626.     if exists (select * from sysobjects
  627.             where type = 'P'
  628.                 and name = 'sp_replproberemoteserver')
  629.         drop procedure sp_replproberemoteserver
  630.         
  631.     if exists (select * from sysobjects
  632.             where type = 'FN'
  633.                 and name = 'fn_getpersistedservernamecasevariation'
  634.                 and user_id('system_function_schema') = uid)
  635.         drop function system_function_schema.fn_getpersistedservernamecasevariation
  636.  
  637. GO
  638.  
  639. /* 
  640. **  We must execute dbo.sp_MSdrop_replcom here since sp_MSdrop_replcom may not exist
  641. **  before this script is applied and it is possible
  642. **  that some replication sps are left. (We ignore error when dropping
  643. **  the replication so it is possible sp_MSdrop_replcom is dropped but
  644. **  some replication sps were left).
  645. */
  646. exec dbo.sp_MSdrop_replcom
  647. go
  648.  
  649. raiserror('Creating procedure sp_MScreate_distributor_tables', 0,1)
  650. GO
  651.  
  652. create procedure sp_MScreate_distributor_tables
  653. as
  654.     declare @profile_id     int
  655.     declare @retcode    int
  656.     declare @profile_name nvarchar(100)
  657.     declare @profile_desc nvarchar(100)
  658.  
  659.     /* Create MSpublishers table */
  660.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSdistpublishers'
  661.         and xtype = 'U')
  662.     begin
  663.         DROP TABLE msdb..MSdistpublishers
  664.         if @@error<> 0 goto FAILURE
  665.     end
  666.  
  667.     /* Create MSdistributiondbs table */
  668.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSdistributiondbs'
  669.         and xtype = 'U')
  670.     begin
  671.         DROP TABLE msdb..MSdistributiondbs
  672.         if @@error<> 0 goto FAILURE
  673.     end
  674.  
  675.     /* create MSdistributor table */
  676.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSdistributor'
  677.         and xtype = 'U')
  678.     begin
  679.         DROP TABLE msdb..MSdistributor
  680.         if @@error<> 0 goto FAILURE
  681.     end
  682.  
  683.     /* create sysreplicationalerts table */
  684.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'sysreplicationalerts'
  685.         and xtype = 'U')
  686.     begin
  687.         DROP TABLE msdb..sysreplicationalerts
  688.         if @@error<> 0 goto FAILURE
  689.     end
  690.     
  691.     /* create MSagent_profiles table */
  692.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSagent_profiles'
  693.         and xtype = 'U')
  694.     begin
  695.         DROP TABLE msdb..MSagent_profiles
  696.         if @@error<> 0 goto FAILURE
  697.     end
  698.  
  699.     /* create MSagent_parameters table */
  700.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSagent_parameters' 
  701.         and xtype = 'U')
  702.     begin
  703.         DROP TABLE msdb..MSagent_parameters
  704.         if @@error<> 0 goto FAILURE
  705.     end
  706.  
  707.     /* create MSdatatype_mappings table */
  708.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSdatatype_mappings' 
  709.         and xtype = 'U')
  710.     begin
  711.         DROP TABLE msdb..MSdatatype_mappings
  712.         if @@error<> 0 goto FAILURE
  713.     end
  714.  
  715.     
  716.     CREATE TABLE msdb.dbo.MSdistpublishers
  717.     (
  718.         name                sysname     NOT NULL,
  719.         distribution_db     sysname     NOT NULL,
  720.         working_directory   nvarchar(255) NOT NULL,
  721.         security_mode       int         NOT NULL,
  722.         login               sysname     NOT NULL,
  723.         password            nvarchar(524) NULL,
  724.         active              bit         NOT NULL,
  725.         trusted             bit         NOT NULL,
  726.         thirdparty_flag     bit         NOT NULL
  727.     )
  728.     if @@error<> 0 goto FAILURE
  729.  
  730.     CREATE UNIQUE CLUSTERED INDEX uc1MSdistpublishers 
  731.         ON msdb.dbo.MSdistpublishers(name)
  732.     if @@error<> 0 goto FAILURE
  733.  
  734.     exec msdb.dbo.sp_MS_marksystemobject MSdistpublishers
  735.  
  736.     CREATE TABLE msdb.dbo.MSdistributiondbs
  737.     (
  738.         name            sysname     NOT NULL,
  739.         min_distretention   int     NOT NULL,
  740.         max_distretention   int     NOT NULL,
  741.         history_retention   int     NOT NULL
  742.     )
  743.     if @@error<> 0 goto FAILURE
  744.  
  745.     CREATE UNIQUE CLUSTERED INDEX uc1MSdistributiondbs 
  746.             ON msdb.dbo.MSdistributiondbs(name)
  747.     if @@error<> 0 goto FAILURE
  748.  
  749.     exec msdb.dbo.sp_MS_marksystemobject MSdistributiondbs
  750.  
  751.  
  752.     CREATE TABLE msdb.dbo.MSdistributor 
  753.     ( 
  754.         property        sysname     NOT NULL, 
  755.         value           nvarchar(3000)  NULL
  756.     )
  757.     if @@error<> 0 goto FAILURE
  758.     
  759.     CREATE UNIQUE CLUSTERED INDEX uc1MSdistributor 
  760.             ON msdb.dbo.MSdistributor(property)
  761.     if @@error<> 0 goto FAILURE
  762.     exec msdb.dbo.sp_MS_marksystemobject MSdistributor
  763.  
  764.     CREATE TABLE msdb.dbo.sysreplicationalerts
  765.     (
  766.         alert_id            int identity(1,1) NOT NULL,
  767.         status              int NOT NULL,
  768.         agent_type          int NULL,
  769.         agent_id            int NULL,
  770.         error_id            int NULL,
  771.         alert_error_code    int NULL,
  772.         time                datetime NOT NULL,
  773.         publisher           sysname NULL,
  774.         publisher_db        sysname NULL,
  775.         publication         sysname NULL,
  776.         publication_type    int NULL,
  777.         subscriber          sysname NULL,
  778.         subscriber_db       sysname NULL,
  779.         article             sysname NULL,
  780.         destination_object  sysname NULL,
  781.         source_object       sysname NULL,
  782.         alert_error_text    ntext NULL
  783.     )
  784.     if @@error<> 0 goto FAILURE
  785.  
  786.     CREATE UNIQUE CLUSTERED INDEX ucsysreplicationalerts ON msdb.dbo.sysreplicationalerts(alert_id)
  787.     if @@error<> 0 goto FAILURE
  788.     exec msdb.dbo.sp_MS_marksystemobject sysreplicationalerts
  789.  
  790.     CREATE TABLE msdb.dbo.MSagent_profiles
  791.     (
  792.         profile_id    int           NOT NULL IDENTITY,
  793.         profile_name  sysname       NOT NULL,
  794.         agent_type          int             NOT NULL, -- 1-Snapshot, 2-Logreader,
  795.                                                       -- 3-Distribution, 4-Merge,
  796.                                                       -- 9-Qreader
  797.         type                int             NOT NULL, -- 0-System, 1-Custom
  798.         description         nvarchar(3000)  NULL,
  799.         def_profile   bit               NOT NULL
  800.     )
  801.     if @@error<> 0 goto FAILURE
  802.  
  803.  
  804.     CREATE UNIQUE CLUSTERED INDEX ucMSagent_profiles ON msdb.dbo.MSagent_profiles
  805.         (profile_name, profile_id, agent_type)
  806.     if @@error<> 0 goto FAILURE
  807.     exec msdb.dbo.sp_MS_marksystemobject MSagent_profiles
  808.  
  809.     CREATE TABLE msdb.dbo.MSagent_parameters
  810.     (
  811.         profile_id     int          NOT NULL,
  812.         parameter_name       sysname        NOT NULL,
  813.         value                nvarchar(255)  NOT NULL
  814.     )
  815.     if @@error<> 0 goto FAILURE
  816.  
  817.     CREATE UNIQUE CLUSTERED INDEX ucMSagent_parameters ON msdb.dbo.MSagent_parameters
  818.         (parameter_name, profile_id)
  819.     if @@error<> 0 goto FAILURE
  820.     
  821.     exec msdb.dbo.sp_MS_marksystemobject MSagent_parameters
  822.     /* 
  823.     ** Create default / non default profiles 
  824.     ** for all the agents 
  825.     */
  826.  
  827.     /* 
  828.     ** Snapshot agent 
  829.     */
  830.     set @profile_id = NULL
  831.     set @profile_name = formatmessage(20545) -- Default Snapshot Profile
  832.     set @profile_desc = NULL
  833.  
  834.     exec @retcode = dbo.sp_add_agent_profile
  835.             @profile_id = @profile_id OUT,
  836.             @profile_name = @profile_name,
  837.             @agent_type = 1,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  838.             @profile_type = 0,   -- 0-System, 1-Custom 
  839.             @description = @profile_desc,
  840.             @default = 1
  841.     if (@retcode = 1 or @@ERROR <> 0)
  842.         goto FAILURE
  843.  
  844.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  845.     if (@retcode = 1 or @@ERROR <> 0)
  846.         goto FAILURE
  847.  
  848.     /* 
  849.     ** Logreader agent 
  850.     */
  851.     set @profile_id = NULL
  852.     set @profile_name = formatmessage(20545) -- Default LogReader Profile
  853.     set @profile_desc = NULL
  854.  
  855.     exec @retcode = dbo.sp_add_agent_profile
  856.             @profile_id = @profile_id OUT,
  857.             @profile_name = @profile_name,
  858.             @agent_type = 2,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  859.             @profile_type = 0,   -- 0-System, 1-Custom 
  860.             @description = @profile_desc,
  861.             @default = 1
  862.     if (@retcode = 1 or @@ERROR <> 0)
  863.         goto FAILURE
  864.  
  865.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  866.     if (@retcode = 1 or @@ERROR <> 0)
  867.         goto FAILURE
  868.  
  869.     /* 
  870.     ** Logreader agent - Verbose History Profile
  871.     */
  872.     set @profile_id = NULL
  873.     set @profile_name = formatmessage(20546) -- LogReader Verbose History Profile
  874.     set @profile_desc = formatmessage(20547)
  875.  
  876.     exec @retcode = dbo.sp_add_agent_profile
  877.             @profile_id = @profile_id OUT,
  878.             @profile_name = @profile_name,
  879.             @agent_type = 2,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  880.             @profile_type = 0,   -- 0-System, 1-Custom 
  881.             @description = @profile_desc,
  882.             @default = 0
  883.     if (@retcode = 1 or @@ERROR <> 0)
  884.         goto FAILURE
  885.  
  886.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  887.     if (@retcode = 1 or @@ERROR <> 0)
  888.         goto FAILURE
  889.  
  890.     /* 
  891.     ** Distribution agent 
  892.     */
  893.     set @profile_id = NULL
  894.     set @profile_name = formatmessage(20545) -- Default Distribution Profile
  895.     set @profile_desc = NULL
  896.  
  897.     exec @retcode = dbo.sp_add_agent_profile
  898.             @profile_id = @profile_id OUT,
  899.             @profile_name = @profile_name,
  900.             @agent_type = 3,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  901.             @profile_type = 0,   -- 0-System, 1-Custom 
  902.             @description = @profile_desc,
  903.             @default = 1
  904.     if (@retcode = 1 or @@ERROR <> 0)
  905.         goto FAILURE
  906.  
  907.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  908.     if (@retcode = 1 or @@ERROR <> 0)
  909.         goto FAILURE
  910.  
  911.     /*
  912.     ** Distribution Agent Verbose History Profile
  913.     */
  914.     set @profile_id = NULL
  915.     set @profile_name = formatmessage(20546) -- Distribution Verbose History Profile
  916.     set @profile_desc = formatmessage(20547)
  917.  
  918.     exec @retcode = dbo.sp_add_agent_profile
  919.             @profile_id = @profile_id OUT,
  920.             @profile_name = @profile_name,
  921.             @agent_type = 3,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  922.             @profile_type = 0,   -- 0-System, 1-Custom 
  923.             @description = @profile_desc,
  924.             @default = 0
  925.     if (@retcode = 1 or @@ERROR <> 0)
  926.         goto FAILURE
  927.  
  928.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  929.     if (@retcode = 1 or @@ERROR <> 0)
  930.         goto FAILURE
  931.  
  932.     /* 
  933.     ** Merge agent : Default profile for well connected scenarios 
  934.     */
  935.     set @profile_id = NULL
  936.     set @profile_name = formatmessage(20545) -- Default Merge Profile
  937.     set @profile_desc = NULL
  938.  
  939.     exec @retcode = dbo.sp_add_agent_profile
  940.             @profile_id = @profile_id OUT,
  941.             @profile_name = @profile_name,
  942.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  943.             @profile_type = 0,   -- 0-System, 1-Custom 
  944.             @description = @profile_desc,
  945.             @default = 1
  946.  
  947.     if (@retcode = 1 or @@ERROR <> 0)
  948.         goto FAILURE
  949.  
  950.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  951.     if (@retcode = 1 or @@ERROR <> 0)
  952.         goto FAILURE
  953.  
  954.     /* 
  955.     ** Merge agent : Non default profile for disconnected scenarios ( unreliable link ) 
  956.     */
  957.     set @profile_id = NULL
  958.     set @profile_name = formatmessage(20548) -- Non-Default Merge Profile
  959.     set @profile_desc = formatmessage(20549)
  960.  
  961.     exec @retcode = dbo.sp_add_agent_profile
  962.             @profile_id = @profile_id OUT,
  963.             @profile_name = @profile_name,
  964.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  965.             @profile_type = 0,   -- 0-System, 1-Custom 
  966.             @description = @profile_desc,
  967.             @default = 0
  968.  
  969.     if (@retcode = 1 or @@ERROR <> 0)
  970.         goto FAILURE
  971.  
  972.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  973.     if (@retcode = 1 or @@ERROR <> 0)
  974.         goto FAILURE
  975.  
  976.     /* 
  977.     ** Merge agent : Non default profile for verbose histroy
  978.     */
  979.     set @profile_id = NULL
  980.     set @profile_name = formatmessage(20546) -- Verbose Merge Profile
  981.     set @profile_desc = formatmessage(20547)
  982.  
  983.     exec @retcode = dbo.sp_add_agent_profile
  984.             @profile_id = @profile_id OUT,
  985.             @profile_name = @profile_name,
  986.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  987.             @profile_type = 0,   -- 0-System, 1-Custom 
  988.             @description = @profile_desc,
  989.             @default = 0
  990.  
  991.     if (@retcode = 1 or @@ERROR <> 0)
  992.         goto FAILURE
  993.  
  994.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  995.     if (@retcode = 1 or @@ERROR <> 0)
  996.         goto FAILURE
  997.  
  998.     /* 
  999.     ** Merge agent : Synchronization Manager Profile
  1000.     */
  1001.     set @profile_id = NULL
  1002.     set @profile_name = formatmessage(20550) -- SyncMgr Profile
  1003.     set @profile_desc = formatmessage(20551)
  1004.  
  1005.     exec @retcode = dbo.sp_add_agent_profile
  1006.             @profile_id = @profile_id OUT,
  1007.             @profile_name = @profile_name,
  1008.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  1009.             @profile_type = 0,   -- 0-System, 1-Custom 
  1010.             @description = @profile_desc,
  1011.             @default = 0
  1012.  
  1013.     if (@retcode = 1 or @@ERROR <> 0)
  1014.         goto FAILURE
  1015.  
  1016.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  1017.     if (@retcode = 1 or @@ERROR <> 0)
  1018.         goto FAILURE
  1019.  
  1020.     /* 
  1021.     ** Distribution agent : Synchronization Manager Profile
  1022.     */
  1023.     set @profile_id = NULL
  1024.     set @profile_name = formatmessage(20550) -- SyncMgr Profile
  1025.     set @profile_desc = formatmessage(20551)
  1026.  
  1027.     exec @retcode = dbo.sp_add_agent_profile
  1028.             @profile_id = @profile_id OUT,
  1029.             @profile_name = @profile_name,
  1030.             @agent_type = 3,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  1031.             @profile_type = 0,   -- 0-System, 1-Custom 
  1032.             @description = @profile_desc,
  1033.             @default = 0
  1034.  
  1035.     if (@retcode = 1 or @@ERROR <> 0)
  1036.         goto FAILURE
  1037.  
  1038.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  1039.     if (@retcode = 1 or @@ERROR <> 0)
  1040.         goto FAILURE
  1041.  
  1042.     /* 
  1043.     ** Qreader agent (default profile)
  1044.     */
  1045.     set @profile_id = NULL
  1046.     set @profile_name = formatmessage(20545) -- Default QueueReader Profile
  1047.     set @profile_desc = formatmessage(20589)
  1048.  
  1049.     exec @retcode = dbo.sp_add_agent_profile
  1050.             @profile_id = @profile_id OUT,
  1051.             @profile_name = @profile_name,
  1052.             @agent_type = 9,
  1053.             @profile_type = 0, 
  1054.             @description = @profile_desc,
  1055.             @default = 1
  1056.     if (@retcode = 1 or @@ERROR <> 0)
  1057.         goto FAILURE
  1058.  
  1059.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  1060.     if (@retcode = 1 or @@ERROR <> 0)
  1061.         goto FAILURE
  1062.  
  1063.     /* 
  1064.     ** Merge agent : Rowcount Validation profile  
  1065.     */
  1066.     set @profile_id = NULL
  1067.     set @profile_name = formatmessage(21308) -- Rowcount Validation Profile
  1068.     set @profile_desc = formatmessage(21309) -- Rowcount Validation Profile Description
  1069.  
  1070.     exec @retcode = dbo.sp_add_agent_profile
  1071.             @profile_id = @profile_id OUT,
  1072.             @profile_name = @profile_name,
  1073.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  1074.             @profile_type = 0,   -- 0-System, 1-Custom 
  1075.             @description = @profile_desc,
  1076.             @default = 0
  1077.  
  1078.     if (@retcode = 1 or @@ERROR <> 0)
  1079.         goto FAILURE
  1080.  
  1081.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  1082.     if (@retcode = 1 or @@ERROR <> 0)
  1083.         goto FAILURE
  1084.  
  1085.     /* 
  1086.     ** Merge agent : Rowcount & Checksum Validation profile  
  1087.     */
  1088.     set @profile_id = NULL
  1089.     set @profile_name = formatmessage(21310) -- Rowcount & Checksum Validation Profile
  1090.     set @profile_desc = formatmessage(21311) -- Rowcount & Checksum Validation Profile Description
  1091.  
  1092.     exec @retcode = dbo.sp_add_agent_profile
  1093.             @profile_id = @profile_id OUT,
  1094.             @profile_name = @profile_name,
  1095.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  1096.             @profile_type = 0,   -- 0-System, 1-Custom 
  1097.             @description = @profile_desc,
  1098.             @default = 0
  1099.  
  1100.     if (@retcode = 1 or @@ERROR <> 0)
  1101.         goto FAILURE
  1102.  
  1103.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  1104.     if (@retcode = 1 or @@ERROR <> 0)
  1105.         goto FAILURE
  1106.  
  1107.  
  1108.     /* 
  1109.     ** Distribution agent, skip error profile
  1110.     */
  1111.     set @profile_id = NULL
  1112.     set @profile_name = formatmessage(20599) -- Default Distribution Profile
  1113.     set @profile_desc = formatmessage(20600)
  1114.  
  1115.     exec @retcode = dbo.sp_add_agent_profile
  1116.             @profile_id = @profile_id OUT,
  1117.             @profile_name = @profile_name,
  1118.             @agent_type = 3,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  1119.             @profile_type = 0,   -- 0-System, 1-Custom 
  1120.             @description = @profile_desc,
  1121.             @default = 0
  1122.     if (@retcode = 1 or @@ERROR <> 0)
  1123.         goto FAILURE
  1124.  
  1125.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  1126.     if (@retcode = 1 or @@ERROR <> 0)
  1127.         goto FAILURE
  1128.  
  1129.     /* 
  1130.     ** Merge agent : High volume server-to-server profile
  1131.     */
  1132.     set @profile_id = NULL
  1133.     set @profile_name = formatmessage(20616) -- High volume server-to-server profile
  1134.     set @profile_desc = formatmessage(20617) -- High volume server-to-server profile Description
  1135.  
  1136.     exec @retcode = dbo.sp_add_agent_profile
  1137.             @profile_id = @profile_id OUT,
  1138.             @profile_name = @profile_name,
  1139.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  1140.             @profile_type = 0,   -- 0-System, 1-Custom 
  1141.             @description = @profile_desc,
  1142.             @default = 0
  1143.  
  1144.     if (@retcode = 1 or @@ERROR <> 0)
  1145.         goto FAILURE
  1146.  
  1147.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  1148.     if (@retcode = 1 or @@ERROR <> 0)
  1149.         goto FAILURE
  1150.  
  1151.     create table msdb.dbo.MSdatatype_mappings 
  1152.     (
  1153.     dbms_name           sysname NOT NULL,
  1154.     sql_type            sysname NOT NULL,
  1155.     dest_type           sysname NOT NULL,
  1156.     dest_prec           int     NOT NULL,
  1157.     dest_create_params  int     NOT NULL,
  1158.     dest_nullable       bit     NOT NULL
  1159.     )
  1160.     exec msdb.dbo.sp_MS_marksystemobject MSdatatype_mappings
  1161.     -- MS Jet
  1162.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'binary' , 'binary', 255, 4, 1
  1163.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'varbinary' , 'varbinary', 255, 4, 1
  1164.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'binary' , 'image', 1073741824, 0, 1
  1165.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'varbinary' , 'image', 1073741824, 0, 1
  1166.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'sql_variant' , 'longtext', 1073741824, 0, 1
  1167.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'varchar' , 'varchar', 255, 4, 1
  1168.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'varchar' , 'longtext', 1073741824, 0, 1
  1169.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'nchar' , 'nchar', 255, 4, 1
  1170.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'nchar' , 'longtext', 1073741824, 0, 1
  1171.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'char' , 'char', 255, 4, 1
  1172.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'char' , 'longtext', 1073741824, 0, 1
  1173.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'nvarchar' , 'nchar varying', 255, 4, 1
  1174.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'nvarchar' , 'longtext', 1073741824, 0, 1
  1175.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'datetime' , 'datetime', 255, 0, 1
  1176.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'smalldatetime' , 'datetime', 255, 0, 1
  1177.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'decimal' , 'decimal', 255, 3, 1
  1178.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'numeric' , 'decimal', 255, 3, 1
  1179.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'float' , 'float', 255, 0, 1
  1180.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'real' , 'real', 255, 0, 1
  1181.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'bigint' , 'decimal', 255, 0, 1
  1182.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'int' , 'int', 255, 0, 1
  1183.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'smallint' , 'smallint', 255, 0, 1
  1184.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'tinyint' , 'byte', 255, 0, 1
  1185.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'money' , 'currency', 255, 0, 1
  1186.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'smallmoney' , 'currency', 255, 0, 1
  1187.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'bit' , 'bit', 255, 0, 1
  1188.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'sysname' , 'nchar varying', 255, 4, 1
  1189.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'timestamp' , 'binary', 255, 4, 1
  1190.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'uniqueidentifier' , 'guid', 255, 0, 1
  1191.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'text' , 'longtext', 1073741824, 0, 1
  1192.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'ntext' , 'longtext', 1073741824, 0, 1
  1193.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'image' , 'image', 1073741824, 0, 1
  1194.  
  1195.     -- Oracle
  1196.     exec dbo.sp_add_datatype_mapping 'Oracle', 'binary' , 'raw', 255, 4, 1
  1197.     exec dbo.sp_add_datatype_mapping 'Oracle', 'varbinary' , 'raw', 255, 4, 1
  1198.     exec dbo.sp_add_datatype_mapping 'Oracle', 'binary' , 'long raw', 2147483647, 0, 1
  1199.     exec dbo.sp_add_datatype_mapping 'Oracle', 'varbinary' , 'long raw', 2147483647, 0, 1
  1200.     exec dbo.sp_add_datatype_mapping 'Oracle', 'sql_variant' , 'long', 2147483647, 0, 1
  1201.     --exec dbo.sp_add_datatype_mapping 'Oracle', 'varchar' , 'char', 255, 4, 1
  1202.     exec dbo.sp_add_datatype_mapping 'Oracle', 'varchar' , 'varchar2', 2000, 4, 1
  1203.     exec dbo.sp_add_datatype_mapping 'Oracle', 'varchar' , 'long', 2147483647, 0, 1
  1204.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nchar' , 'char', 255, 4, 1
  1205.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nchar' , 'varchar2', 2000, 4, 1
  1206.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nchar' , 'long', 2147483647, 0, 1
  1207.     exec dbo.sp_add_datatype_mapping 'Oracle', 'char' , 'char', 255, 4, 1
  1208.     exec dbo.sp_add_datatype_mapping 'Oracle', 'char' , 'varchar2', 2000, 4, 1
  1209.     exec dbo.sp_add_datatype_mapping 'Oracle', 'char' , 'long', 2147483647, 0, 1
  1210.     --exec dbo.sp_add_datatype_mapping 'Oracle', 'nvarchar' , 'char', 255, 4, 1
  1211.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nvarchar' , 'varchar2', 2000, 4, 1
  1212.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nvarchar' , 'long', 2147483647, 0, 1
  1213.     exec dbo.sp_add_datatype_mapping 'Oracle', 'datetime' , 'date', 255, 0, 1
  1214.     exec dbo.sp_add_datatype_mapping 'Oracle', 'smalldatetime' , 'date', 255, 0, 1
  1215.     exec dbo.sp_add_datatype_mapping 'Oracle', 'decimal' , 'number', 255, 3, 1
  1216.     exec dbo.sp_add_datatype_mapping 'Oracle', 'numeric' , 'number', 255, 3, 1
  1217.     exec dbo.sp_add_datatype_mapping 'Oracle', 'float' , 'float', 255, 0, 1
  1218.     exec dbo.sp_add_datatype_mapping 'Oracle', 'real' , 'float', 255, 0, 1
  1219.     exec dbo.sp_add_datatype_mapping 'Oracle', 'bigint' , 'number', 255, 3, 1
  1220.     exec dbo.sp_add_datatype_mapping 'Oracle', 'int' , 'number', 255, 3, 1
  1221.     exec dbo.sp_add_datatype_mapping 'Oracle', 'smallint' , 'number', 255, 3, 1
  1222.     exec dbo.sp_add_datatype_mapping 'Oracle', 'tinyint' , 'number', 255, 3, 1
  1223.     exec dbo.sp_add_datatype_mapping 'Oracle', 'money' , 'number', 255, 3, 1
  1224.     exec dbo.sp_add_datatype_mapping 'Oracle', 'smallmoney' , 'number', 255, 3, 1
  1225.     exec dbo.sp_add_datatype_mapping 'Oracle', 'bit' , 'number', 255, 3, 1
  1226.     exec dbo.sp_add_datatype_mapping 'Oracle', 'sysname' , 'char', 255, 4, 1
  1227.     exec dbo.sp_add_datatype_mapping 'Oracle', 'timestamp' , 'raw', 255, 4, 1
  1228.     exec dbo.sp_add_datatype_mapping 'Oracle', 'uniqueidentifier' , 'char', 255, 4, 1
  1229.     exec dbo.sp_add_datatype_mapping 'Oracle', 'text' , 'long', 2147483647, 0, 1
  1230.     exec dbo.sp_add_datatype_mapping 'Oracle', 'ntext' , 'long', 2147483647, 0, 1
  1231.     exec dbo.sp_add_datatype_mapping 'Oracle', 'image' , 'long raw', 2147483647, 0, 1
  1232.  
  1233.     -- MS SSCE
  1234.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'binary' , 'binary', 255, 4, 1
  1235.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'varbinary' , 'varbinary', 255, 4, 1
  1236.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'binary' , 'image', 1073741824, 0, 1
  1237.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'varbinary' , 'image', 1073741824, 0, 1
  1238.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'sql_variant' , 'ntext', 1073741824, 0, 1
  1239.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'varchar' , 'national char varying', 255, 4, 1
  1240.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'varchar' , 'ntext', 1073741824, 0, 1
  1241.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'nchar' , 'nchar', 255, 4, 1
  1242.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'nchar' , 'ntext', 1073741824, 0, 1
  1243.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'char' , 'nchar', 255, 4, 1
  1244.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'char' , 'ntext', 1073741824, 0, 1
  1245.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'nvarchar' , 'national char varying', 255, 4, 1
  1246.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'nvarchar' , 'ntext', 1073741824, 0, 1
  1247.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'datetime' , 'datetime', 255, 0, 1
  1248.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'smalldatetime' , 'datetime', 255, 0, 1
  1249.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'decimal' , 'numeric', 255, 3, 1
  1250.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'numeric' , 'numeric', 255, 3, 1
  1251.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'float' , 'float', 255, 0, 1
  1252.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'real' , 'real', 255, 0, 1
  1253.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'bigint' , 'bigint', 255, 0, 1
  1254.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'int' , 'int', 255, 0, 1
  1255.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'smallint' , 'smallint', 255, 0, 1
  1256.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'tinyint' , 'tinyint', 255, 0, 1
  1257.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'money' , 'money', 255, 0, 1
  1258.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'smallmoney' , 'money', 255, 0, 1
  1259.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'bit' , 'bit', 255, 0, 1
  1260.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'sysname' , 'national char varying', 255, 4, 1
  1261.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'timestamp' , 'binary', 255, 4, 1
  1262.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'uniqueidentifier' , 'uniqueidentifier', 255, 0, 1
  1263.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'text' , 'ntext', 1073741824, 0, 1
  1264.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'ntext' , 'ntext', 1073741824, 0, 1
  1265.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'image' , 'image', 1073741824, 0, 1
  1266.  
  1267.     --DB2/400
  1268.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'bit', 'SMALLINT', 1,  0, 1
  1269.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'tinyint', 'SMALLINT', 3, 0, 1
  1270.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'smallint', 'SMALLINT', 5, 0, 1
  1271.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'int', 'INT', 10, 0, 1
  1272.  
  1273.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'char', 'CHAR', 8000, 4, 1
  1274.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'varchar', 'VARCHAR', 8000, 4, 1
  1275.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'smalldatetime', 'TIMESTAMP', 26, 0, 1
  1276.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'datetime', 'TIMESTAMP', 26, 0, 1
  1277.     
  1278.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'real', 'REAL', 24, 0, 1
  1279.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'decimal', 'DECIMAL', 31, 3, 1
  1280.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'double precision', 'DOUBLE', 53, 0, 1
  1281.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'float', 'FLOAT', 53, 0, 1
  1282.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'numeric', 'NUMERIC', 31, 3, 1
  1283.  
  1284.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'smallmoney', 'DECIMAL', 10, 3, 1
  1285.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'money', 'DECIMAL', 19, 3, 1
  1286.  
  1287.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'varbinary', 'VARCHAR () FOR BIT DATA', 8000, 4, 1
  1288.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'binary', 'CHAR () FOR BIT DATA', 8000, 4, 1
  1289.  
  1290.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'timestamp', 'CHAR () FOR BIT DATA', 8, 4, 1
  1291.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'uniqueidentifier', 'CHAR', 38, 4, 1
  1292.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'image', 'VARCHAR () FOR BIT DATA', 32739, 4, 1
  1293.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'text', 'VARCHAR', 32739, 4, 1
  1294.  
  1295.     --DB2/MVS
  1296.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'bit', 'SMALLINT', 1,  0, 1
  1297.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'tinyint', 'SMALLINT', 3, 0, 1
  1298.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'smallint', 'SMALLINT', 5, 0, 1
  1299.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'int', 'INT', 10, 0, 1
  1300.  
  1301.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'char', 'CHAR', 254, 4, 1
  1302.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'varchar', 'VARCHAR', 4045, 4, 1
  1303.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'char', 'VARCHAR', 4045, 4, 1
  1304.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'smalldatetime', 'TIMESTAMP', 26, 0, 1
  1305.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'datetime', 'TIMESTAMP', 26, 0, 1
  1306.  
  1307.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'real', 'REAL', 24, 0, 1
  1308.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'decimal', 'DECIMAL', 31, 3, 1
  1309.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'double precision', 'DOUBLE', 53, 0, 1
  1310.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'float', 'FLOAT', 53, 0, 1
  1311.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'numeric', 'NUMERIC', 31, 3, 1
  1312.  
  1313.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'smallmoney', 'DECIMAL', 10, 3, 1
  1314.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'money', 'DECIMAL', 19, 3, 1
  1315.  
  1316.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'varbinary', 'VARCHAR () FOR BIT DATA', 4045, 4, 1
  1317.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'binary', 'CHAR () FOR BIT DATA', 254, 4, 1
  1318.  
  1319.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'timestamp', 'CHAR () FOR BIT DATA', 8, 4, 1
  1320.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'uniqueidentifier', 'CHAR', 38, 4, 1
  1321.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'image', 'VARCHAR () FOR BIT DATA', 4045, 4, 1
  1322.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'text', 'VARCHAR', 4045, 4, 1
  1323.  
  1324.     --DB2/NT
  1325.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'bit', 'SMALLINT', 1,  0, 1
  1326.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'tinyint', 'SMALLINT', 3, 0, 1
  1327.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'smallint', 'SMALLINT', 5, 0, 1
  1328.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'int', 'INT', 10, 0, 1
  1329.  
  1330.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'char', 'CHAR', 254, 4, 1
  1331.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'varchar', 'VARCHAR', 4000, 4, 1
  1332.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'char', 'VARCHAR', 4000, 4, 1
  1333.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'smalldatetime', 'TIMESTAMP', 26, 0, 1
  1334.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'datetime', 'TIMESTAMP', 26, 0, 1
  1335.  
  1336.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'real', 'REAL', 24, 0, 1
  1337.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'decimal', 'DECIMAL', 31, 3, 1
  1338.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'double precision', 'DOUBLE', 53, 0, 1
  1339.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'float', 'FLOAT', 53, 0, 1
  1340.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'numeric', 'NUMERIC', 31, 3, 1
  1341.  
  1342.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'smallmoney', 'DECIMAL', 10, 3, 1
  1343.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'money', 'DECIMAL', 19, 3, 1
  1344.  
  1345.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'varbinary', 'VARCHAR () FOR BIT DATA', 4000, 4, 1
  1346.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'binary', 'CHAR () FOR BIT DATA', 254, 4, 1
  1347.  
  1348.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'timestamp', 'CHAR () FOR BIT DATA', 8, 4, 1
  1349.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'uniqueidentifier', 'CHAR', 38, 4, 1
  1350.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'image', 'VARCHAR () FOR BIT DATA', 4000, 4, 1
  1351.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'text', 'VARCHAR', 4000, 4, 1
  1352.  
  1353.     --DB2/6000
  1354.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'bit', 'SMALLINT', 1,  0, 1
  1355.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'tinyint', 'SMALLINT', 3, 0, 1
  1356.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'smallint', 'SMALLINT', 5, 0, 1
  1357.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'int', 'INT', 10, 0, 1
  1358.  
  1359.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'char', 'CHAR', 254, 4, 1
  1360.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'varchar', 'VARCHAR', 4000, 4, 1
  1361.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'char', 'VARCHAR', 4000, 4, 1
  1362.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'smalldatetime', 'TIMESTAMP', 26, 0, 1
  1363.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'datetime', 'TIMESTAMP', 26, 0, 1
  1364.  
  1365.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'real', 'REAL', 24, 0, 1
  1366.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'decimal', 'DECIMAL', 31, 3, 1
  1367.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'double precision', 'DOUBLE', 53, 0, 1
  1368.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'float', 'FLOAT', 53, 0, 1
  1369.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'numeric', 'NUMERIC', 31, 3, 1
  1370.  
  1371.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'smallmoney', 'DECIMAL', 10, 3, 1
  1372.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'money', 'DECIMAL', 19, 3, 1
  1373.  
  1374.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'varbinary', 'VARCHAR () FOR BIT DATA', 4000, 4, 1
  1375.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'binary', 'CHAR () FOR BIT DATA', 254, 4, 1
  1376.  
  1377.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'timestamp', 'CHAR () FOR BIT DATA', 8, 4, 1
  1378.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'uniqueidentifier', 'CHAR', 38, 4, 1
  1379.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'image', 'VARCHAR () FOR BIT DATA', 4000, 4, 1
  1380.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'text', 'VARCHAR', 4000, 4, 1
  1381.  
  1382.     return 0
  1383. FAILURE:
  1384.     return 1
  1385. go
  1386.  
  1387. dump tran master with no_log
  1388. GO
  1389.  
  1390. /*
  1391. ** Create replication stored procedures and functions.
  1392. ** Part 2:  create all other stored procedures and functions.
  1393. */
  1394. --
  1395. -- Name: fn_getpersistedservernamecasevariation
  1396. --
  1397. -- Description: A simple function for getting the case variation of a
  1398. --              server name as persisted in master..sysservers. For
  1399. --              example, if sErVeRnAmE is what is being persisted 
  1400. --              as a srvname in sysservers
  1401. --
  1402. --              select fn_getpersistedservernamecasevariation('SERVERNAME')
  1403. --              select fn_getpersistedservernamecasevariation('servername')
  1404. --            
  1405. --              will all return the string 'sErVeRnAmE'                
  1406. --
  1407. -- Parameter: @servername sysname (mandatory)
  1408. --
  1409. -- Notes: This function will return null if the specified server name
  1410. --        does not match any srvname's in master..sysservers in a
  1411. --        case insensitive manner.
  1412. --
  1413. -- Security: Admin access only
  1414. -- 
  1415. raiserror('Creating function fn_getpersistedservernamecasevariation', 0,1)
  1416. go
  1417. create function system_function_schema.fn_getpersistedservernamecasevariation (
  1418.     @servername sysname
  1419.     ) returns sysname
  1420. as
  1421. begin
  1422.  
  1423.     declare @real_servername sysname
  1424.     select @real_servername = null
  1425.     select @real_servername = srvname 
  1426.       from master.dbo.sysservers
  1427.      where upper(@servername) = upper(srvname) collate database_default 
  1428.  
  1429.     return @real_servername
  1430. end
  1431. go
  1432.  
  1433. raiserror('Creating procedure sp_MSIfExistsRemoteLogin', 0,1)
  1434. GO
  1435.  
  1436. CREATE proc sp_MSIfExistsRemoteLogin
  1437.     @remotesrvname sysname,
  1438.     @locallgname   sysname,
  1439.     @remotelgname  sysname
  1440. AS
  1441. begin
  1442.  
  1443.  
  1444.     if (@locallgname IS NULL)
  1445.     begin
  1446.      IF EXISTS (SELECT * FROM master.dbo.sysremotelogins srl, master.dbo.sysservers ss
  1447.                  WHERE UPPER(ss.srvname) = UPPER(@remotesrvname) collate database_default AND 
  1448.                        srl.remoteserverid = ss.srvid AND 
  1449.                        (srl.remoteusername = @remotelgname OR 
  1450.                        (srl.remoteusername IS NULL AND srl.sid = 0x2)))
  1451.         return (1)
  1452.     else
  1453.         return (0)
  1454.     end
  1455.  
  1456.     if exists (SELECT * FROM master.dbo.sysremotelogins srl, master.dbo.sysservers ss 
  1457.                                 WHERE UPPER(ss.srvname) = UPPER(@remotesrvname) collate database_default AND 
  1458.                                       srl.remoteserverid = ss.srvid AND 
  1459.                                       srl.remoteusername = @remotelgname AND 
  1460.                                       srl.sid = suser_sid(@locallgname))
  1461.         return (1)
  1462.     else
  1463.         return (0)
  1464. end
  1465. go
  1466.  
  1467. raiserror('Creating procedure sp_helppublicationsync', 0,1)
  1468. GO
  1469.  
  1470. CREATE PROCEDURE sp_helppublicationsync (
  1471.         @publication sysname    /* The publication name */
  1472.         ) AS
  1473.     SET NOCOUNT ON
  1474.     RAISERROR (21023, 16, -1,'sp_helppublicationsync')
  1475.     RETURN(1)
  1476. GO
  1477.  
  1478. raiserror('Creating procedure sp_MSreplrole', 0,1)
  1479. GO
  1480. create procedure sp_MSreplrole
  1481. @name sysname,
  1482. @operation nvarchar(4)
  1483. as
  1484. declare @retcode int
  1485. -- Add/Drop when proper.
  1486. if @operation = 'add'
  1487. begin
  1488.     if user_id(@name) is null
  1489.     begin
  1490.         exec @retcode = dbo.sp_addrole @name
  1491.         IF @@ERROR <> 0 or @retcode <> 0
  1492.             RETURN (1)
  1493.     end
  1494. end
  1495. else if @operation = 'drop' 
  1496. begin
  1497.     if user_id(@name) is not null
  1498.     begin
  1499.         exec @retcode = dbo.sp_droprole @name
  1500.         IF @@ERROR <> 0 or @retcode <> 0
  1501.             RETURN (1)
  1502.     end
  1503. end
  1504. go
  1505.  
  1506. dump tran master with no_log
  1507. GO
  1508.  
  1509. raiserror('Creating procedure sp_addpublication_snapshot', 0,1)
  1510. GO
  1511.  
  1512. CREATE PROCEDURE sp_addpublication_snapshot (
  1513.     @publication sysname,    
  1514.     @frequency_type  int = 4 ,              /* 4== Daily */
  1515.     @frequency_interval int = 1,            /* Every day */
  1516.     @frequency_subday int = 4,              /* Sub interval = Minute */
  1517.     @frequency_subday_interval int = 5,     /* Every five minutes */
  1518.     @frequency_relative_interval int = 1, 
  1519.     @frequency_recurrence_factor int = 0, 
  1520.     @active_start_date int = 0, 
  1521.     @active_end_date int = 99991231 , 
  1522.     @active_start_time_of_day int = 0, 
  1523.     @active_end_time_of_day int = 235959,
  1524.     @snapshot_job_name nvarchar(100) = NULL         
  1525. ) AS
  1526.  
  1527.     SET NOCOUNT ON
  1528.  
  1529.     /*
  1530.     ** Declarations.
  1531.     */
  1532.     DECLARE @retcode                int
  1533.     DECLARE @newid                  int
  1534.     DECLARE @mergepublish_bit       smallint
  1535.     DECLARE @transpublish_bit       int
  1536.     DECLARE @transpub_found         bit
  1537.     DECLARE @mergepub_found         bit
  1538.     DECLARE @newagentid             int
  1539.  
  1540.     /*
  1541.     ** Initializations
  1542.     */
  1543.     select @mergepublish_bit    = 4
  1544.     select @transpublish_bit    = 1
  1545.     select @transpub_found      = 0
  1546.     select @mergepub_found      = 0
  1547.     
  1548.     /*
  1549.     ** Parameter Check
  1550.     */
  1551.     exec @retcode = dbo.sp_MSreplcheck_name @publication
  1552.     if @@ERROR <> 0 or @retcode <> 0
  1553.         return(1)
  1554.  
  1555.     /*
  1556.     ** Security Check
  1557.     */
  1558.     exec @retcode = dbo.sp_MSreplcheck_publish
  1559.     if @@ERROR <> 0 or @retcode <> 0
  1560.         return(1)
  1561.  
  1562.     /*
  1563.     **  Check if the publication is valid.
  1564.     **  1. Check transaction-level publications
  1565.     **  2. Check merge publications
  1566.     */
  1567.     if (select category & @transpublish_bit from master..sysdatabases where name = DB_NAME() collate database_default) <> 0
  1568.     begin
  1569.         EXEC @retcode = dbo.sp_MSaddpub_snapshot     @publication ,    
  1570.             @frequency_type,  
  1571.             @frequency_interval, 
  1572.             @frequency_subday, 
  1573.             @frequency_subday_interval,
  1574.             @frequency_relative_interval, 
  1575.             @frequency_recurrence_factor, 
  1576.             @active_start_date, 
  1577.             @active_end_date, 
  1578.             @active_start_time_of_day, 
  1579.             @active_end_time_of_day,         
  1580.             @newagentid OUTPUT,
  1581.             @snapshot_job_name 
  1582.  
  1583.         IF @retcode <> 0 and @@ERROR <> 0 
  1584.             BEGIN
  1585.                 RETURN (1)
  1586.             END
  1587.  
  1588.         if @newagentid <> 0
  1589.             begin
  1590.                 select @transpub_found = 1
  1591.                 goto DONE
  1592.             end             
  1593.  
  1594.     end
  1595.     
  1596.     if (select category & @mergepublish_bit from master..sysdatabases where name = DB_NAME() collate database_default) <> 0
  1597.     begin
  1598.         EXEC @retcode = dbo.sp_MSaddmergepub_snapshot     @publication ,    
  1599.             @frequency_type,  
  1600.             @frequency_interval, 
  1601.             @frequency_subday, 
  1602.             @frequency_subday_interval,
  1603.             @frequency_relative_interval, 
  1604.             @frequency_recurrence_factor, 
  1605.             @active_start_date, 
  1606.             @active_end_date, 
  1607.             @active_start_time_of_day, 
  1608.             @active_end_time_of_day,         
  1609.             @newagentid OUTPUT,
  1610.             @snapshot_job_name
  1611.  
  1612.         IF @retcode <> 0 and @@ERROR <> 0 
  1613.             BEGIN
  1614.                 RETURN (1)
  1615.             END
  1616.  
  1617.         if @newagentid <> 0
  1618.             begin
  1619.                 select @mergepub_found = 1
  1620.                 goto DONE
  1621.             end             
  1622.     end
  1623.  
  1624. DONE:
  1625.     if @transpub_found = 0 and @mergepub_found = 0
  1626.     begin
  1627.         RAISERROR (15001, 11, -1, @publication)
  1628.         RETURN (1)
  1629.     end
  1630.     return (0)        
  1631. GO
  1632.  
  1633. raiserror('Creating procedure sp_MShelpobjectpublications', 0,1)
  1634. GO
  1635.  
  1636. create procedure sp_MShelpobjectpublications (@object_name sysname)
  1637. AS
  1638.     /*
  1639.     ** Declarations.
  1640.     */
  1641.     DECLARE @retcode                int
  1642.     DECLARE @mergepublish_bit       smallint
  1643.     DECLARE @transpublish_bit       int
  1644.  
  1645.     declare @object_id int
  1646.  
  1647.     /*
  1648.     ** Initializations
  1649.     */
  1650.     select @mergepublish_bit    = 4
  1651.     select @transpublish_bit    = 1
  1652.     select @object_id           = OBJECT_ID(@object_name)
  1653.  
  1654.     create table #helpobjpubs (
  1655.         publication     sysname         collate database_default not null, 
  1656.         reptype         int             NOT NULL, 
  1657.         article         sysname         collate database_default not null, 
  1658.         article_type    int             NULL, 
  1659.         column_tracking int             NULL, 
  1660.         article_resolver nvarchar(255)  collate database_default null,
  1661.         identity_support int             NULL, 
  1662.         resolver_clsid nvarchar(50)        NULL,
  1663.         resolver_info  nvarchar(255)    NULL,
  1664.         verify_resolver_signature int     NULL,
  1665.         allow_interactive_resolver bit     NULL,
  1666.         fast_multicol_updateproc bit    NULL,
  1667.         check_permissions int            NULL)
  1668.     /*
  1669.     **  1. Return the transactional publications that the table is involved in
  1670.     */
  1671.     if (select category & @transpublish_bit from master..sysdatabases where name = DB_NAME() collate database_default) <> 0
  1672.     begin
  1673.         if exists (select * from sysextendedarticlesview a, 
  1674.             syspublications p where a.pubid = p.pubid and a.objid = @object_id)
  1675.             begin
  1676.                 insert into #helpobjpubs(publication, reptype, article, article_type) 
  1677.                     select p.name, 1, a.name, a.type  from sysextendedarticlesview a, 
  1678.                         syspublications p where a.pubid = p.pubid and a.objid = @object_id
  1679.                 IF @@ERROR <> 0 
  1680.                     BEGIN
  1681.                         select @retcode = 1
  1682.                         goto DONE
  1683.                     END
  1684.             end
  1685.     end
  1686.     /*
  1687.     **  2. Return the merge publications that the table is involved in
  1688.     */
  1689.     if exists (select * from sysobjects where name='sysmergepublications')
  1690.     begin
  1691.         if exists (select * from sysmergeextendedarticlesview a, 
  1692.             sysmergepublications p where a.pubid = p.pubid and a.objid = @object_id)
  1693.             begin
  1694.                 insert into #helpobjpubs (publication, reptype, article, article_type, column_tracking, article_resolver,
  1695.                 identity_support, resolver_clsid, resolver_info, verify_resolver_signature, allow_interactive_resolver,
  1696.                             fast_multicol_updateproc, check_permissions)
  1697.                     select p.name, 2, a.name, a.type, a.column_tracking, a.article_resolver, a.identity_support,
  1698.                             a.resolver_clsid, a.resolver_info, a.verify_resolver_signature, a.allow_interactive_resolver,
  1699.                             a.fast_multicol_updateproc, a.check_permissions
  1700.                         from sysmergeextendedarticlesview a, sysmergepublications p 
  1701.                         where a.pubid = p.pubid and a.objid = @object_id
  1702.                 IF @@ERROR <> 0 
  1703.                     BEGIN
  1704.                         select @retcode = 1
  1705.                         goto DONE
  1706.                     END
  1707.             end
  1708.     end
  1709.     select @retcode = 0
  1710.  
  1711. DONE:
  1712.     select * from #helpobjpubs
  1713.     drop table #helpobjpubs
  1714.     return (@retcode)
  1715. go
  1716.  
  1717. raiserror('Creating procedure sp_helpreplicationdb', 0,1)
  1718. GO
  1719.  
  1720. CREATE PROCEDURE sp_helpreplicationdb
  1721.         @dbname sysname = '%', @type sysname = 'pub'
  1722.     AS
  1723.  
  1724.     SET NOCOUNT ON
  1725.  
  1726.     /*
  1727.     ** Declarations.
  1728.     */
  1729.  
  1730.     DECLARE @retcode int, @typebit int
  1731.  
  1732.     if (lower(@type collate SQL_Latin1_General_CP1_CS_AS) like 'pub%')
  1733.        select @typebit = 1
  1734.     else if (lower(@type collate SQL_Latin1_General_CP1_CS_AS) like 'sub%')
  1735.        select @typebit = 2
  1736.     else
  1737.     begin
  1738.        raiserror(14091,-1,-1)
  1739.        return 1
  1740.     end
  1741.  
  1742.     /*
  1743.     ** Parameter Check:  @dbname.
  1744.     ** Check to make sure that the database name conforms to the rules
  1745.     ** for identifiers.
  1746.     */
  1747.  
  1748.     IF @dbname <> '%'
  1749.        BEGIN
  1750.           EXECUTE @retcode = dbo.sp_validname @dbname
  1751.  
  1752.           IF @@ERROR <> 0 OR @retcode <> 0
  1753.           RETURN (1)
  1754.        END
  1755.  
  1756.     /*
  1757.     ** Show databases with this option enabled.
  1758.     */
  1759.  
  1760.     SELECT name
  1761.       FROM master..sysdatabases
  1762.      WHERE ((@dbname = N'%') or (name = @dbname collate database_default)) 
  1763.        AND (category & @typebit) <> 0
  1764. go
  1765.  
  1766.  
  1767. raiserror('Creating procedure sp_helpdistributor', 0,1)
  1768. go
  1769.  
  1770. CREATE PROCEDURE sp_helpdistributor (
  1771.     @distributor sysname  = '%' OUTPUT, /* The distribution server name */
  1772.     @distribdb   sysname  = '%' OUTPUT, /* The distribution database */
  1773.     @directory   nvarchar(255) = '%' OUTPUT, /* The working directory */
  1774.     @account     nvarchar(255) = '%' OUTPUT, /* The Windows NT user account */
  1775.     @min_distretention int      = -1 OUTPUT, /* The min distribution retention */
  1776.     @max_distretention int      = -1 OUTPUT, /* The min distribution retention */
  1777.     @history_retention   int  = -1 OUTPUT, /* The history retention period */
  1778.     @history_cleanupagent nvarchar(100) = '%' OUTPUT, /* The history cleanup agent */
  1779.     @distrib_cleanupagent nvarchar(100) = '%' OUTPUT, /* The distribution cleanup agent */
  1780.     @publisher sysname = NULL,  /* Name of publisher */
  1781.     @local nvarchar(5) = NULL,        /* Get local server values */
  1782.     @rpcsrvname sysname = '%' OUTPUT
  1783. ) AS
  1784.     SET NOCOUNT ON
  1785.  
  1786.     /*
  1787.     ** Declarations.
  1788.     */
  1789.     DECLARE @loc_distributor         sysname
  1790.     DECLARE @loc_distribdb             sysname
  1791.     DECLARE @loc_directory             nvarchar(255)
  1792.     DECLARE @loc_account             nvarchar(255)
  1793.     DECLARE @loc_mindistretention     int
  1794.     DECLARE @loc_maxdistretention     int
  1795.     DECLARE @loc_historyretention   int  
  1796.     DECLARE @loc_historycleanupagent nvarchar(100)
  1797.     DECLARE @loc_distribcleanupagent nvarchar(100)
  1798.     DECLARE @loc_security_mode  int  
  1799.     DECLARE @loc_login sysname
  1800.     DECLARE @loc_password sysname
  1801.     declare @loc_rpcsrvname sysname
  1802.     DECLARE @proc nvarchar(255)
  1803.     DECLARE @reg_key nvarchar (255)
  1804.     DECLARE @retcode int
  1805.     declare @rpcsrvlogin sysname
  1806.     declare @srvid smallint
  1807.     declare @dist_rpcname sysname
  1808.     declare @platform_nt binary
  1809.  
  1810.     select @platform_nt = 0x1
  1811.  
  1812.     IF @publisher IS NULL
  1813.     BEGIN
  1814.         /* 
  1815.         ** 6.x compatibility 
  1816.         ** If local is set, we know the call is from a publisher. 
  1817.         ** set it to be @@REMSERVER 
  1818.         ** Otherwise, set it to be local server name
  1819.         ** Note: @@REMSERVER is NULL for local sp calls 
  1820.         */
  1821.         IF LOWER(@local) = 'local' AND @@REMSERVER IS NOT NULL
  1822.             SELECT @publisher = @@REMSERVER
  1823.         ELSE
  1824.             SELECT  @publisher = @@SERVERNAME
  1825.     END
  1826.  
  1827.     /*
  1828.     ** Get the distribution server
  1829.     */
  1830.     SELECT @dist_rpcname = srvname, @loc_distributor = datasource, @srvid = srvid, @loc_rpcsrvname = srvname
  1831.         FROM master..sysservers
  1832.         WHERE srvstatus & 8 <> 0
  1833.     
  1834.     if @loc_distributor is null
  1835.         GOTO DONE
  1836.  
  1837.     select @rpcsrvlogin = name from master.dbo.sysxlogins where
  1838.         srvid = @srvid and sid is NULL
  1839.  
  1840.     /*
  1841.     ** If remote distribuiton, execute dbo.sp_helpdistributor on distribution
  1842.     ** server.
  1843.     */
  1844.     IF UPPER(@loc_distributor) <> UPPER(@@SERVERNAME)
  1845.     BEGIN
  1846.         SELECT @proc = @dist_rpcname + '.master.dbo.sp_helpdistributor'
  1847.         EXECUTE @retcode = @proc
  1848.             @loc_distributor OUTPUT,
  1849.             @loc_distribdb OUTPUT,
  1850.             @loc_directory OUTPUT,
  1851.             @loc_account OUTPUT,
  1852.             @loc_mindistretention OUTPUT,
  1853.             @loc_maxdistretention OUTPUT,
  1854.             @loc_historyretention OUTPUT,
  1855.             @loc_historycleanupagent OUTPUT,
  1856.             @loc_distribcleanupagent OUTPUT,
  1857.             @@SERVERNAME,
  1858.             @local = 'local'
  1859.         IF @retcode <> 0 or @@ERROR <> 0
  1860.            RETURN (1)
  1861.  
  1862.             GOTO DONE
  1863.     END
  1864.  
  1865.     SELECT  @loc_distribdb = distribution_db,
  1866.             @loc_directory = working_directory
  1867.         FROM msdb.dbo.MSdistpublishers WHERE UPPER(name) = UPPER(@publisher) collate database_default
  1868.  
  1869.     IF @@ERROR <> 0
  1870.         RETURN 1 ;
  1871.  
  1872.     SELECT  @loc_mindistretention = min_distretention,      
  1873.             @loc_maxdistretention = max_distretention,
  1874.             @loc_historyretention = history_retention
  1875.         FROM msdb.dbo.MSdistributiondbs WHERE name = @loc_distribdb collate database_default 
  1876.  
  1877.     /*
  1878.     ** Fetch the distribution account name.
  1879.     */
  1880.     IF ((@distributor = '%' AND @distribdb = '%' AND @directory = '%'
  1881.     AND @account = '%' AND @min_distretention = -1 AND @max_distretention = -1 
  1882.     AND @history_retention = -1 AND @history_cleanupagent = '%' 
  1883.     AND @distrib_cleanupagent = '%' ) 
  1884.     OR @account IS NULL) and ( platform() & @platform_nt = @platform_nt ) 
  1885.        BEGIN
  1886.  
  1887.         declare @instance sysname
  1888.         declare @regkey nvarchar(260)
  1889.         select @instance = convert(sysname, SERVERPROPERTY('InstanceName'))
  1890.         select @regkey = 'SYSTEM\CurrentControlSet\Services\'
  1891.         -- default installation
  1892.         if @instance is null
  1893.             SELECT @regkey = @regkey + 'SQLServerAgent' 
  1894.         else
  1895.             SELECT @regkey = @regkey + 'SQLAgent$' + @instance
  1896.  
  1897.         SELECT @proc = 'master..xp_regread'
  1898.         EXECUTE @retcode = @proc 'HKEY_LOCAL_MACHINE',
  1899.               @regkey,
  1900.               'ObjectName',
  1901.             @param = @loc_account OUTPUT
  1902.     
  1903.         IF @@ERROR <> 0 OR @retcode <> 0
  1904.             SELECT @loc_account = NULL
  1905.        END
  1906.  
  1907.     /*
  1908.     ** Fetch the history cleanup agentname.
  1909.     */
  1910.     IF @loc_distribdb IS NOT NULL
  1911.         SELECT @loc_historycleanupagent = formatmessage (20567, @loc_distribdb)
  1912.  
  1913.     /*
  1914.     ** Fetch the distribution cleanup agent name.
  1915.     */
  1916.     IF @loc_distribdb IS NOT NULL
  1917.         SELECT @loc_distribcleanupagent = formatmessage (20568, @loc_distribdb)
  1918.  
  1919.  
  1920. DONE:
  1921.     /*
  1922.     ** Return result set if no output parameters
  1923.     */
  1924.  
  1925.     IF @distributor = '%' AND @distribdb = '%' AND @directory = '%'
  1926.     AND @account = '%' AND @min_distretention = -1 AND @max_distretention = -1 
  1927.     AND @history_retention = -1 AND @history_cleanupagent = '%' 
  1928.     AND @distrib_cleanupagent = '%' AND @rpcsrvname = '%' 
  1929.     SELECT       'distributor'           = @loc_distributor,
  1930.                'distribution database' = @loc_distribdb,
  1931.                'directory'              = @loc_directory,
  1932.                'account'               = @loc_account,
  1933.                'min distrib retention' = @loc_mindistretention,
  1934.                'max distrib retention' = @loc_maxdistretention,
  1935.                'history retention'     = @loc_historyretention,
  1936.                'history cleanup agent'  = @loc_historycleanupagent,
  1937.                'distribution cleanup agent' = @loc_distribcleanupagent,
  1938.                'rpc server name' = @loc_rpcsrvname,
  1939.                'rpc login name' = @rpcsrvlogin
  1940.  
  1941.     /*
  1942.     ** Return output parameters if requested.
  1943.     */
  1944.  
  1945.     IF @distributor IS NULL
  1946.         SELECT @distributor = @loc_distributor
  1947.     IF @distribdb IS NULL
  1948.         SELECT @distribdb = @loc_distribdb
  1949.     IF @directory IS NULL
  1950.         SELECT @directory = @loc_directory
  1951.     IF @account IS NULL
  1952.         SELECT @account = @loc_account
  1953.     IF @min_distretention IS NULL
  1954.         SELECT @min_distretention = @loc_mindistretention
  1955.     IF @max_distretention IS NULL
  1956.         SELECT @max_distretention = @loc_maxdistretention
  1957.     IF @history_retention IS NULL
  1958.         SELECT @history_retention = @loc_historyretention
  1959.     IF @history_cleanupagent IS NULL
  1960.         SELECT @history_cleanupagent = @loc_historycleanupagent
  1961.     IF @distrib_cleanupagent IS NULL
  1962.         SELECT @distrib_cleanupagent = @loc_distribcleanupagent
  1963.    
  1964.     IF @rpcsrvname IS NULL
  1965.     begin
  1966.         -- Use local RPC if possible to avoid blocking problem.
  1967.         if is_srvrolemember('sysadmin') = 1 and UPPER(@loc_distributor) = UPPER(@@servername)
  1968.             select @rpcsrvname = @@servername
  1969.         else
  1970.             select @rpcsrvname = @loc_rpcsrvname
  1971.     end
  1972.   
  1973.     RETURN (0)
  1974. GO
  1975.  
  1976. dump tran master with no_log
  1977. go
  1978.  
  1979. raiserror('Creating procedure sp_enumdsn', 0,1)
  1980. GO
  1981.  
  1982. CREATE PROCEDURE sp_enumdsn
  1983.     AS
  1984.  
  1985.     if 1 <> is_srvrolemember('sysadmin')
  1986.     begin
  1987.         raiserror(15247,-1,-1)
  1988.         return 1
  1989.     end
  1990.  
  1991.     SET NOCOUNT ON
  1992.  
  1993.     DECLARE @distributor sysname
  1994.     DECLARE @distproc nvarchar (300)
  1995.     DECLARE @retcode int
  1996.  
  1997.     DECLARE @dsotype_odbc int
  1998.     DECLARE @dsotype_oledb int
  1999.  
  2000.     select @dsotype_odbc = 1
  2001.     select @dsotype_oledb = 3
  2002.  
  2003.     /*
  2004.     ** Get distribution server information for remote RPC
  2005.     ** subscription calls.
  2006.     */
  2007.  
  2008.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT
  2009.     IF @@error <> 0 OR @retcode <> 0 or @distributor is null
  2010.         BEGIN
  2011.         RAISERROR (14071, 16, -1)
  2012.             RETURN (1)
  2013.     END
  2014.  
  2015.     create table #datasourcestemptable (DataSourceName sysname collate database_default not null, Description nvarchar(255) collate database_default null, DataSourceType int null, ProviderName nvarchar(255) collate database_default null)
  2016.     
  2017.     /*
  2018.     ** Call xp_enumdsn
  2019.     */
  2020.     SELECT @distproc = RTRIM(@distributor) + '.master..xp_enumdsn'
  2021.     insert into #datasourcestemptable(DataSourceName, Description) EXEC @retcode = @distproc
  2022.     IF @@error <> 0
  2023.         BEGIN
  2024.         RAISERROR (14071, 16, -1)
  2025.         RETURN (1)
  2026.     END
  2027.  
  2028.     update #datasourcestemptable set DataSourceType = @dsotype_odbc where DataSourceType is null
  2029.     IF @@error <> 0 
  2030.     BEGIN
  2031.         RETURN (1)
  2032.     END
  2033.  
  2034.     /*
  2035.     ** Call sp_enumoledbdatasources
  2036.     */
  2037.     SELECT @distproc = RTRIM(@distributor) + '.master.dbo.sp_enumoledbdatasources'
  2038.     insert into #datasourcestemptable(DataSourceName, Description, ProviderName) EXEC @retcode = @distproc
  2039.     IF @@error <> 0
  2040.         BEGIN
  2041.         RAISERROR (14071, 16, -1)
  2042.         RETURN (1)
  2043.     END
  2044.  
  2045.     update #datasourcestemptable set DataSourceType = @dsotype_oledb where DataSourceType is null
  2046.     IF @@error <> 0 
  2047.     BEGIN
  2048.         RETURN (1)
  2049.     END
  2050.     
  2051.     select 'Data Source Name' = DataSourceName, Description, 'Type' = DataSourceType, 'Provider Name' = ProviderName 
  2052.     from #datasourcestemptable
  2053.     order by 3, 1
  2054.  
  2055.     drop table #datasourcestemptable
  2056.  
  2057.     return (0)
  2058. go
  2059.  
  2060. raiserror('Creating procedure sp_enumoledbdatasources', 0,1)
  2061. GO
  2062.  
  2063. CREATE PROCEDURE sp_enumoledbdatasources
  2064.     AS
  2065.     set nocount on
  2066.  
  2067.     select srvname, srvproduct, providername from master..sysservers where 
  2068.     (srvstatus & 0x0080) <> 0
  2069.  
  2070.     return (0)
  2071. go
  2072.  
  2073. raiserror('Creating procedure sp_helpsubscriberinfo', 0,1)
  2074. GO
  2075.  
  2076. CREATE PROCEDURE sp_helpsubscriberinfo
  2077.     @subscriber sysname = '%'
  2078.     AS
  2079.  
  2080.     SET NOCOUNT ON
  2081.  
  2082.     DECLARE @distributor sysname
  2083.     DECLARE @distribdb sysname
  2084.     DECLARE @distproc nvarchar (300)
  2085.     DECLARE @retcode int
  2086.     DECLARE @subscriber_bit smallint
  2087.     DECLARE @show_password bit
  2088.  
  2089.     /*
  2090.     ** Security Check
  2091.     */
  2092.  
  2093.     /*
  2094.     ** Initializations.
  2095.     */
  2096.     SELECT @subscriber_bit = 4
  2097.  
  2098.  
  2099.     /*
  2100.     ** Check if subscriber is valid
  2101.     */
  2102.  
  2103.     IF @subscriber IS NULL
  2104.         BEGIN
  2105.             RAISERROR (14043, 16, -1, '@subscriber')
  2106.             RETURN (1)
  2107.         END
  2108.  
  2109.  
  2110.     IF @subscriber <> '%'
  2111.         BEGIN
  2112.  
  2113.             EXECUTE @retcode = dbo.sp_validname @subscriber
  2114.             IF @retcode <> 0
  2115.                 RETURN (1)
  2116.  
  2117.             IF NOT EXISTS (SELECT *
  2118.                         FROM master..sysservers
  2119.                         WHERE UPPER(srvname) = UPPER(@subscriber) collate database_default
  2120.                         AND (srvstatus & @subscriber_bit) <> 0)
  2121.                 BEGIN
  2122.                     RAISERROR (14010, 16, -1)
  2123.                     RETURN (1)
  2124.                 END
  2125.         END
  2126.  
  2127.  
  2128.     /*
  2129.     ** Get distribution server information for remote RPC
  2130.     ** subscription calls.
  2131.     */
  2132.  
  2133.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  2134.                                        @distribdb   = @distribdb OUTPUT
  2135.  
  2136.     /* RAISEERROR is subscriber is not '%' */                                               
  2137.     IF @@error <> 0 OR @retcode <> 0 OR @distribdb IS NULL OR @distributor IS NULL
  2138.         BEGIN
  2139.         IF @subscriber <> '%'
  2140.             BEGIN
  2141.                 RAISERROR (14071, 16, -1)
  2142.                 RETURN (1)
  2143.             END
  2144.         ELSE
  2145.             RETURN 0
  2146.         END
  2147.  
  2148.      create table #subscriber_info
  2149.      (
  2150.         publisher  sysname collate database_default not null,
  2151.         subscriber  sysname collate database_default not null,
  2152.         type tinyint not null,           /* 0: MS SQL Server 1: ODBC Data Source */
  2153.         login sysname collate database_default null, 
  2154.         password nvarchar(524) NULL,
  2155.         commit_batch_size int not null,  -- commit_batch_size, no longer supported
  2156.         status_batch_size int not null,  -- status_batch_size, no longer supported
  2157.         flush_frequency int not null,  -- flush_frequency, no longer supported
  2158.         frequency_type int not null,
  2159.         frequency_interval int not null,
  2160.         frequency_relative_interval int not null,
  2161.         frequency_recurrence_factor int not null,
  2162.         frequency_subday int not null,
  2163.         frequency_subday_interval int not null,
  2164.         active_start_time_of_day int not null,
  2165.         active_end_time_of_day int not null,
  2166.         active_start_date int not null,
  2167.         active_end_date int not null,
  2168.         retryattempt    int not null, -- retryattempt, no longer exist
  2169.         retrydelay  int not null,  -- retrydelay, no longer exist
  2170.         description nvarchar(255) collate database_default null,
  2171.         security_mode int not null,
  2172.         frequency_type2 int not null,
  2173.         frequency_interval2 int not null,
  2174.         frequency_relative_interval2 int not null,
  2175.         frequency_recurrence_factor2 int not null,
  2176.         frequency_subday2 int not null,
  2177.         frequency_subday_interval2 int not null,
  2178.         active_start_time_of_day2 int not null,
  2179.         active_end_time_of_day2 int not null,
  2180.         active_start_date2 int not null,
  2181.         active_end_date2 int not null
  2182.     )
  2183.  
  2184.     IF @@error <> 0
  2185.         RETURN (1)
  2186.      
  2187.     /*
  2188.     ** Retrieve MSsubscriber_info
  2189.     */
  2190.     if is_srvrolemember('sysadmin') = 1
  2191.         select @show_password = 1
  2192.     else
  2193.         select @show_password = 0
  2194.  
  2195.     SELECT @distproc = RTRIM(@distributor) + '.' +
  2196.         RTRIM(@distribdb) + '.dbo.sp_MShelp_subscriber_info'
  2197.  
  2198.     insert into #subscriber_info EXEC @retcode = @distproc 
  2199.         @publisher = @@SERVERNAME, 
  2200.         @subscriber = @subscriber,
  2201.         @show_password = @show_password
  2202.  
  2203.     IF @@error <> 0
  2204.         BEGIN
  2205.         RAISERROR (14071, 16, -1)
  2206.         RETURN (1)
  2207.     END
  2208.  
  2209.     IF @retcode <> 0
  2210.         BEGIN
  2211.         RAISERROR (14085, 16, -1)
  2212.         RETURN (1)
  2213.      END
  2214.  
  2215.     -- Filter out subscribers that are not defined locally but left at the distributor
  2216.     -- This will happen if the publisher is cleaned up when the distributor is offline.
  2217.     select info.* from #subscriber_info info, master..sysservers servers where
  2218.         UPPER(info.subscriber) = UPPER(servers.srvname) collate database_default and
  2219.         (servers.srvstatus & @subscriber_bit) <> 0
  2220.  
  2221. go
  2222.  
  2223. raiserror('Creating procedure sp_replica', 0,1)
  2224. GO
  2225.  
  2226. CREATE PROCEDURE sp_replica (
  2227.         @tabname nvarchar(92),     /* The table being replicated */
  2228.         @replicated nvarchar(5)    /* True or false */
  2229.         ) AS
  2230.  
  2231.     SET NOCOUNT ON
  2232.     RAISERROR (21023, 16, -1,'sp_replica')
  2233.     RETURN(1)
  2234. go
  2235.  
  2236.  
  2237. dump tran master with no_log
  2238. go
  2239.  
  2240. raiserror('Creating procedure sp_addpublisher', 0,1)
  2241. GO
  2242.  
  2243. CREATE PROCEDURE sp_addpublisher (
  2244.     @publisher sysname,      /* publisher server name */
  2245.     @type nvarchar (5) = NULL     /* NULL or 'dist' */
  2246.  ) AS
  2247.  
  2248.     SET NOCOUNT ON
  2249.     RAISERROR (21023, 16, -1,'sp_addpublisher')
  2250.     RETURN(1)
  2251. go
  2252.  
  2253. raiserror('Creating procedure sp_addsubscriber', 0,1)
  2254. GO
  2255.  
  2256. CREATE PROCEDURE sp_addsubscriber (
  2257.     @subscriber sysname,
  2258.     @type tinyint = 0,
  2259.     @login sysname = 'sa',
  2260.     @password sysname = NULL,
  2261.     @commit_batch_size int = 100,
  2262.     @status_batch_size int = 100,
  2263.     @flush_frequency int = 0,
  2264.     @frequency_type int = 64,
  2265.     @frequency_interval int = 1,
  2266.     @frequency_relative_interval int = 1,
  2267.     @frequency_recurrence_factor int = 0,
  2268.     @frequency_subday int = 4,
  2269.     @frequency_subday_interval int = 5,
  2270.     @active_start_time_of_day int = 0,
  2271.     @active_end_time_of_day int = 235959,
  2272.     @active_start_date int = 0,
  2273.     @active_end_date int = 99991231,
  2274.     @description nvarchar (255) = NULL,
  2275.     @security_mode int = 1,  /* backward compatible */ /* 0 standard; 1 integrated */
  2276.     @encrypted_password bit = 0
  2277.         ) AS
  2278.  
  2279.     DECLARE @distributor sysname
  2280.     DECLARE @distribdb sysname
  2281.     DECLARE @distproc nvarchar (300)
  2282.     DECLARE @retcode int
  2283.     DECLARE @dsn_subscriber tinyint
  2284.     DECLARE @jet_subscriber tinyint
  2285.     DECLARE @oledb_subscriber tinyint
  2286.     DECLARE @dist_rpcname sysname
  2287.     DECLARE @platform_nt binary
  2288.  
  2289.     -- Defined in sqlrepl.h
  2290.     select @dsn_subscriber = 1    /* Const: subscriber type 'dsn' */ 
  2291.     select @jet_subscriber = 2   
  2292.     select @oledb_subscriber = 3  
  2293.     
  2294.     select @platform_nt = 0x1
  2295.  
  2296.     /* 
  2297.     ** Check if replication components are installed on this server
  2298.     */
  2299.     exec @retcode = dbo.sp_MS_replication_installed
  2300.     if (@retcode <> 1)
  2301.     begin
  2302.         return (1)
  2303.     end
  2304.     
  2305.     /*
  2306.     ** Parameter Check:  @subscriber.
  2307.     ** Check to make sure that the subscriber doesn't already exist, and
  2308.     ** that the name is a valid non-null identifier.
  2309.     */
  2310.  
  2311.     IF @subscriber IS NULL
  2312.         BEGIN
  2313.             RAISERROR (14043, 16, -1, '@subscriber')
  2314.             RETURN (1)
  2315.         END
  2316.  
  2317.     EXECUTE @retcode = dbo.sp_validname @subscriber
  2318.  
  2319.     IF @@ERROR <> 0 OR @retcode <> 0
  2320.     RETURN (1)
  2321.  
  2322.     if LOWER(@subscriber) = 'all'
  2323.         BEGIN
  2324.             RAISERROR (14032, 16, -1, '@subscriber')
  2325.             RETURN (1)
  2326.         END
  2327.  
  2328.     /* 
  2329.     ** Check for invalid security mode
  2330.     */
  2331.     IF @security_mode < 0 OR @security_mode > 1
  2332.         BEGIN
  2333.             RAISERROR(14109, 16, -1)
  2334.             RETURN (1)
  2335.         END
  2336.  
  2337.     IF (UPPER(@subscriber) = UPPER(@@SERVERNAME) and @platform_nt != ( platform() & @platform_nt ) and @security_mode = 1)
  2338.         BEGIN
  2339.             RAISERROR(21038, 16, -1)
  2340.             RETURN (1)
  2341.         END
  2342.  
  2343.     IF EXISTS (SELECT *
  2344.                  FROM master..sysservers
  2345.                 WHERE UPPER(srvname) = UPPER(@subscriber) collate database_default
  2346.                   AND srvstatus & 4 <> 0)
  2347.  
  2348.         BEGIN
  2349.             RAISERROR (14040, 16, -1, @subscriber)
  2350.             RETURN (1)
  2351.         END
  2352.  
  2353.     IF @password = N''
  2354.         select @password = NULL
  2355.  
  2356.     
  2357.     /*
  2358.     **  If no MSsubscriber_info parameters skip RPC code.
  2359.     */
  2360.  
  2361.     IF @frequency_type = -1
  2362.         GOTO ADDSUB
  2363.  
  2364.     /*
  2365.     ** Get distribution server information for remote RPC
  2366.     ** subscription calls.
  2367.     */
  2368.  
  2369.  /*   BEGIN TRAN addsubscriber */
  2370.  
  2371.     EXEC @retcode = dbo.sp_helpdistributor  @distributor = @distributor OUTPUT,
  2372.                                         @rpcsrvname = @dist_rpcname OUTPUT,
  2373.                                         @distribdb   = @distribdb   OUTPUT
  2374.  
  2375.     IF @@error <> 0
  2376.         BEGIN
  2377.          RAISERROR (14071, 16, -1)
  2378.          goto undo
  2379.      END
  2380.  
  2381.     IF @retcode <> 0 OR @distribdb IS NULL OR @distributor IS NULL
  2382.         BEGIN
  2383.             RAISERROR (14071, 16, -1)
  2384.             goto undo
  2385.         END
  2386.  
  2387.  
  2388.     DECLARE @zeroint int
  2389.     SELECT @zeroint = 0
  2390.     /*
  2391.     ** Insert information into MSsubscriber_info
  2392.     */
  2393.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSadd_subscriber_info'
  2394.     EXEC @retcode = @distproc
  2395.          @@SERVERNAME,
  2396.          @subscriber,
  2397.      @type,
  2398.      @login,
  2399.      @password,
  2400.      @commit_batch_size,
  2401.      @status_batch_size,
  2402.      @flush_frequency,
  2403.      @frequency_type,
  2404.      @frequency_interval,
  2405.      @frequency_relative_interval,
  2406.      @frequency_recurrence_factor,
  2407.      @frequency_subday,
  2408.      @frequency_subday_interval,
  2409.      @active_start_time_of_day,
  2410.      @active_end_time_of_day,
  2411.      @active_start_date,
  2412.      @active_end_date,
  2413.      /* Work around of server RPC  named parameter problem */
  2414.      @retryattempts = @zeroint,
  2415.      @retrydelay  = @zeroint,
  2416.      @description = @description,
  2417.      @security_mode = @security_mode,
  2418.      @encrypted_password = @encrypted_password
  2419.  
  2420.     IF @@error <> 0 OR @retcode <> 0
  2421.         BEGIN
  2422.         RAISERROR (14042, 16, -1)
  2423.         goto undo
  2424.     END
  2425.  
  2426.     /*
  2427.     ** add schedule information into MSsubscriber_schedule for merge agent
  2428.     */
  2429.     -- NOTE: We may need better default schedule for merge agent
  2430.     
  2431.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSadd_subscriber_schedule'
  2432.     EXEC @retcode = @distproc
  2433.          @@SERVERNAME,
  2434.          @subscriber,
  2435.          0, -- agent_type = 0 means distribution agent
  2436.          @frequency_type,
  2437.          @frequency_interval,
  2438.          @frequency_relative_interval,
  2439.          @frequency_recurrence_factor,
  2440.          @frequency_subday,
  2441.          @frequency_subday_interval,
  2442.          @active_start_time_of_day,
  2443.          @active_end_time_of_day,
  2444.          @active_start_date,
  2445.          @active_end_date
  2446.     
  2447.     IF @@error <> 0 OR @retcode <> 0
  2448.         BEGIN
  2449.             RAISERROR (14042, 16, -1)
  2450.             goto undo
  2451.         END
  2452.  
  2453.     EXEC @retcode = @distproc
  2454.          @@SERVERNAME,
  2455.          @subscriber,
  2456.          1,             --agent_type = 0 means merge agent
  2457.          4,             --frequency_type,
  2458.          1,             --frequency_interval,
  2459.          1,             --frequency_relative_interval,
  2460.          0,             --frequency_recurrence_factor,
  2461.          8,             --frequency_subday,
  2462.          1,             --frequency_subday_interval,
  2463.          0,             --active_start_time_of_day,
  2464.          235959,        --active_end_time_of_day,
  2465.          0,             --active_start_date,
  2466.          99991231       --active_end_date
  2467.     
  2468.     IF @@error <> 0 OR @retcode <> 0
  2469.         BEGIN
  2470.             RAISERROR (14042, 16, -1)
  2471.             goto undo
  2472.         END
  2473.  
  2474. /*    Commit Transaction addsubscriber */
  2475.  
  2476. ADDSUB:
  2477.  
  2478.     /*
  2479.     ** The server may already be listed in master..sysservers, but might
  2480.     ** not be marked as a subscriber yet.  If it's not in
  2481.     ** master..sysservers, let's add it first.
  2482.     */
  2483.  
  2484.     IF NOT EXISTS (SELECT *
  2485.                          FROM master..sysservers
  2486.                         WHERE LOWER(srvname) = LOWER(@subscriber) collate database_default)
  2487.     begin
  2488.  
  2489.         EXECUTE @retcode = dbo.sp_addserver @subscriber
  2490.  
  2491.             IF @@error <> 0 OR @retcode <> 0
  2492.                 BEGIN
  2493.                 RAISERROR (14042, 16, -1)
  2494.                     RETURN (1)
  2495.                 END
  2496.     end
  2497.     ELSE
  2498.     BEGIN
  2499.         SELECT @subscriber = fn_getpersistedservernamecasevariation(@subscriber) collate database_default
  2500.     END
  2501.  
  2502.     /*
  2503.     ** Set the server option to indicate this is a subscriber.
  2504.     */
  2505.  
  2506.     EXECUTE @retcode = dbo.sp_serveroption @subscriber, 'sub', true
  2507.  
  2508.     IF @@error <> 0 OR @retcode <> 0
  2509.         BEGIN
  2510.            RAISERROR (14042, 16, -1)
  2511.            RETURN (1)
  2512.         END
  2513.  
  2514.     /*
  2515.     ** Set the server option to indicate this is a DSN subscriber.
  2516.     */
  2517.     if @type = @dsn_subscriber OR @type = @jet_subscriber OR @type = @oledb_subscriber
  2518.        BEGIN
  2519.           update master..sysservers set srvproduct = 'MSREPL-NONSQL' where
  2520.             UPPER(srvname) = UPPER(@subscriber) collate database_default
  2521.           IF @@error <> 0 
  2522.             return(1)
  2523.        END
  2524.     
  2525.         RETURN (0) 
  2526. undo:
  2527.  
  2528.     RETURN (1) 
  2529. GO
  2530.  
  2531. raiserror('Creating procedure sp_addsubscriber_schedule', 0,1)
  2532. GO
  2533.  
  2534. CREATE PROCEDURE sp_addsubscriber_schedule (
  2535.     @subscriber sysname,
  2536.     @agent_type smallint = 0,
  2537.     @frequency_type int = 64,
  2538.     @frequency_interval int = 1,
  2539.     @frequency_relative_interval int = 1,
  2540.     @frequency_recurrence_factor int = 0,
  2541.     @frequency_subday int = 4,
  2542.     @frequency_subday_interval int = 5,
  2543.     @active_start_time_of_day int = 0,
  2544.     @active_end_time_of_day int = 235959,
  2545.     @active_start_date int = 0,
  2546.     @active_end_date int = 99991231
  2547.         ) AS
  2548.  
  2549.     DECLARE @distributor sysname
  2550.     DECLARE @distribdb sysname
  2551.     DECLARE @distproc nvarchar (300)
  2552.     DECLARE @retcode int
  2553.     DECLARE @msg nvarchar(255)
  2554.     
  2555.     /*
  2556.     ** Parameter Check:  @subscriber.
  2557.     ** Check to make sure that the subscriber doesn't already exist, and
  2558.     ** that the name is a valid non-null identifier.
  2559.     */
  2560.  
  2561.     IF @subscriber IS NULL
  2562.         BEGIN
  2563.             RAISERROR (14043, 16, -1, '@subscriber')
  2564.             RETURN (1)
  2565.         END
  2566.  
  2567.     EXECUTE @retcode = dbo.sp_validname @subscriber
  2568.  
  2569.     IF @@ERROR <> 0 OR @retcode <> 0
  2570.     RETURN (1)
  2571.  
  2572.     if LOWER(@subscriber) = 'all'
  2573.         BEGIN
  2574.             RAISERROR (14032, 16, -1, '@subscriber')
  2575.             RETURN (1)
  2576.         END
  2577.  
  2578.     IF NOT EXISTS (SELECT * FROM master..sysservers WHERE UPPER(srvname) = UPPER(@subscriber) collate database_default)
  2579.         BEGIN
  2580.             RAISERROR (14048, 16, -1, @subscriber)
  2581.             RETURN (1)
  2582.         END
  2583.     
  2584.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  2585.                                        @distribdb   = @distribdb   OUTPUT
  2586.  
  2587.     IF @@error <> 0
  2588.         BEGIN
  2589.          RAISERROR (14071, 16, -1)
  2590.          RETURN (1)
  2591.      END
  2592.  
  2593.     IF @retcode <> 0 OR @distribdb IS NULL OR @distributor IS NULL
  2594.         BEGIN
  2595.             RAISERROR (14071, 16, -1)
  2596.             RETURN (1)
  2597.         END
  2598.     
  2599.     SELECT @distproc = RTRIM(@distributor) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSadd_subscriber_schedule'
  2600.     EXEC @retcode = @distproc
  2601.          @@SERVERNAME,
  2602.          @subscriber,
  2603.          @agent_type,
  2604.          @frequency_type,
  2605.          @frequency_interval,
  2606.          @frequency_relative_interval,
  2607.          @frequency_recurrence_factor,
  2608.          @frequency_subday,
  2609.          @frequency_subday_interval,
  2610.          @active_start_time_of_day,
  2611.          @active_end_time_of_day,
  2612.          @active_start_date,
  2613.          @active_end_date
  2614.     
  2615.     IF @@error <> 0 OR @retcode <> 0
  2616.         BEGIN
  2617.         RAISERROR (14042, 16, -1)
  2618.         RETURN (1)
  2619.     END
  2620.  
  2621.     RETURN (0)
  2622. GO
  2623.  
  2624. raiserror('Creating procedure sp_changesubscriber', 0,1)
  2625. GO
  2626.  
  2627. CREATE PROCEDURE sp_changesubscriber (
  2628.     @subscriber sysname,
  2629.     @type tinyint = NULL,
  2630.     @login sysname = NULL,
  2631.     @password sysname = '%',
  2632.     @commit_batch_size int = NULL,
  2633.     @status_batch_size int = NULL,
  2634.     @flush_frequency int = NULL,
  2635.     @frequency_type int = NULL,
  2636.     @frequency_interval int = NULL,
  2637.     @frequency_relative_interval int = NULL,
  2638.     @frequency_recurrence_factor int = NULL,
  2639.     @frequency_subday int = NULL,
  2640.     @frequency_subday_interval int = NULL,
  2641.     @active_start_time_of_day int = NULL,
  2642.     @active_end_time_of_day int = NULL,
  2643.     @active_start_date int = NULL,
  2644.     @active_end_date int = NULL,
  2645.     @description nvarchar (255) = NULL,
  2646.     @security_mode int = NULL /* 0 standard; 1 integrated */
  2647.  
  2648.         ) AS
  2649.  
  2650.     DECLARE @distributor sysname
  2651.     DECLARE @distribdb sysname
  2652.     DECLARE @distproc nvarchar (300)
  2653.     DECLARE @msg nvarchar(255)
  2654.     DECLARE @retcode int
  2655.     DECLARE @platform_nt binary
  2656.  
  2657.     select @platform_nt = 0x1
  2658.  
  2659.     IF (UPPER(@subscriber) = UPPER(@@SERVERNAME) and @platform_nt != ( platform() & @platform_nt ) and @security_mode = 1)
  2660.         BEGIN
  2661.             RAISERROR(21038, 16, -1)
  2662.             RETURN (1)
  2663.         END
  2664.  
  2665.     /*
  2666.     ** Check to make sure that the subscriber exists.
  2667.     */
  2668.     IF NOT EXISTS (SELECT *
  2669.                  FROM master..sysservers
  2670.                 WHERE UPPER(srvname) = UPPER(@subscriber) collate database_default)
  2671.         BEGIN
  2672.             RAISERROR(14048, 16, 1, @subscriber)
  2673.             RETURN (1)
  2674.         END
  2675.  
  2676.     /*
  2677.     ** Get distribution server information for remote RPC
  2678.     ** subscription calls.
  2679.     */
  2680.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  2681.         @distribdb = @distribdb OUTPUT
  2682.     IF @@ERROR <> 0 OR @retcode <> 0
  2683.         BEGIN
  2684.             RAISERROR (14071, 16, -1)
  2685.             RETURN (1)
  2686.         END
  2687.  
  2688.     DECLARE @intnull int
  2689.     /*
  2690.     ** Update MSsubscriber_info
  2691.     */
  2692.     SELECT @distproc = RTRIM(@distributor) + '.' +
  2693.         RTRIM(@distribdb) + '.dbo.sp_MSupdate_subscriber_info'
  2694.     EXEC @retcode = @distproc
  2695.         @@SERVERNAME,
  2696.         @subscriber,
  2697.     @type,
  2698.     @login,
  2699.     @password,
  2700.     @commit_batch_size,
  2701.     @status_batch_size,
  2702.     @flush_frequency,
  2703.     @frequency_type,
  2704.     @frequency_interval,
  2705.     @frequency_relative_interval,
  2706.     @frequency_recurrence_factor,
  2707.     @frequency_subday,
  2708.     @frequency_subday_interval,
  2709.     @active_start_time_of_day,
  2710.     @active_end_time_of_day,
  2711.     @active_start_date,
  2712.     @active_end_date,
  2713.     @retryattempts = @intnull,
  2714.     @retrydelay  = @intnull,
  2715.     @description = @description,
  2716.     @security_mode = @security_mode
  2717.     IF @@ERROR <> 0 OR @retcode <> 0
  2718.         BEGIN
  2719.             RAISERROR (14048, 16, -1, @subscriber)
  2720.             RETURN (1)
  2721.     END
  2722. go
  2723.  
  2724. raiserror('Creating procedure sp_changesubscriber_schedule', 0,1)
  2725. GO
  2726.  
  2727. CREATE PROCEDURE sp_changesubscriber_schedule (
  2728.     @subscriber sysname,
  2729.     @agent_type smallint,
  2730.     @frequency_type int = NULL,
  2731.     @frequency_interval int = NULL,
  2732.     @frequency_relative_interval int = NULL,
  2733.     @frequency_recurrence_factor int = NULL,
  2734.     @frequency_subday int = NULL,
  2735.     @frequency_subday_interval int = NULL,
  2736.     @active_start_time_of_day int = NULL,
  2737.     @active_end_time_of_day int = NULL,
  2738.     @active_start_date int = NULL,
  2739.     @active_end_date int = NULL
  2740.         ) AS
  2741.  
  2742.     DECLARE @distributor sysname
  2743.     DECLARE @distribdb sysname
  2744.     DECLARE @distproc nvarchar (300)
  2745.     DECLARE @msg nvarchar(255)
  2746.     DECLARE @retcode int
  2747.  
  2748.     /*
  2749.     ** Check to make sure that the subscriber DOES exist.
  2750.     */
  2751.     IF NOT EXISTS (SELECT *
  2752.                  FROM master..sysservers
  2753.                 WHERE UPPER(srvname) = UPPER(@subscriber) collate database_default)
  2754.         BEGIN
  2755.             RAISERROR (14048, 16, -1, @subscriber)
  2756.             RETURN (1)
  2757.         END
  2758.  
  2759.     /*
  2760.     ** Get distribution server information for remote RPC
  2761.     ** subscription calls.
  2762.     */
  2763.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  2764.         @distribdb = @distribdb OUTPUT
  2765.     IF @@ERROR <> 0 OR @retcode <> 0
  2766.          BEGIN
  2767.             RAISERROR (14071, 16, -1)
  2768.              RETURN (1)
  2769.          END
  2770.  
  2771.     /*
  2772.     ** Update MSsubscriber_info
  2773.     */
  2774.     SELECT @distproc = RTRIM(@distributor) + '.' +
  2775.         RTRIM(@distribdb) + '.dbo.sp_MSupdate_subscriber_schedule'
  2776.     EXEC @retcode = @distproc
  2777.         @@SERVERNAME,
  2778.         @subscriber,
  2779.         @agent_type,
  2780.         @frequency_type,
  2781.         @frequency_interval,
  2782.         @frequency_relative_interval,
  2783.         @frequency_recurrence_factor,
  2784.         @frequency_subday,
  2785.         @frequency_subday_interval,
  2786.         @active_start_time_of_day,
  2787.         @active_end_time_of_day,
  2788.         @active_start_date,
  2789.         @active_end_date
  2790.     
  2791.     IF @@ERROR <> 0 OR @retcode <> 0
  2792.         BEGIN
  2793.             RAISERROR (14048, 16, -1, @subscriber)
  2794.             RETURN (1)
  2795.         END
  2796.         
  2797.     RETURN (0)  
  2798. go 
  2799.     
  2800. raiserror('Creating procedure sp_distcounters', 0,1)
  2801. GO
  2802.  
  2803. CREATE PROCEDURE sp_distcounters
  2804.     AS
  2805.  
  2806.     SET NOCOUNT ON
  2807.  
  2808.     /*
  2809.     ** Declarations.
  2810.     */
  2811.     DECLARE @distributor sysname
  2812.     DECLARE @distribdb sysname
  2813.     DECLARE @distproc nvarchar (300)
  2814.     DECLARE @retcode int
  2815.  
  2816.     /*
  2817.     ** Get distribution server information for remote RPC
  2818.     ** subscription calls.  If no distribution information, assume
  2819.     ** replication is not being used.
  2820.     */
  2821.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  2822.                                        @distribdb = @distribdb OUTPUT
  2823.     IF @@error <> 0 OR @retcode <> 0 OR @distributor IS NULL OR
  2824.        @distribdb IS NULL
  2825.     RETURN (1)
  2826.  
  2827.     /*
  2828.     ** Request counters from Distribution Server
  2829.     */
  2830.     SELECT @distproc = RTRIM(@distributor) + '.' + RTRIM(@distribdb) +
  2831.     '.dbo.sp_MSdistribution_counters'
  2832.     EXEC @retcode = @distproc @@SERVERNAME
  2833. go
  2834.  
  2835.  
  2836. raiserror('Creating procedure sp_droppublisher', 0,1)
  2837. GO
  2838.  
  2839. CREATE PROCEDURE sp_droppublisher (
  2840.     @publisher sysname,         /* publisher server name */
  2841.     @type nvarchar (5) = NULL      /* NULL or 'dist' */
  2842.         ) AS
  2843.  
  2844.     SET NOCOUNT ON
  2845.     RAISERROR (21023, 16, -1,'sp_droppublisher')
  2846.     RETURN(1)
  2847.  
  2848. go
  2849.  
  2850. raiserror('Creating procedure sp_dropsubscriber', 0,1)
  2851. GO
  2852.  
  2853. CREATE PROCEDURE sp_dropsubscriber (
  2854.     @subscriber sysname,        /* The name of the subscriber */
  2855.     @reserved nvarchar(50) = NULL,
  2856.     @ignore_distributor bit = 0
  2857.         ) AS
  2858.  
  2859.     SET NOCOUNT ON
  2860.  
  2861.     /*
  2862.     ** Declarations.
  2863.     */
  2864.  
  2865.     DECLARE @db_name sysname
  2866.     DECLARE @foundSubscription int
  2867.     DECLARE @command nvarchar(255)
  2868.     DECLARE @transpublishdb_bit int
  2869.     DECLARE @mergepublishdb_bit int
  2870.     DECLARE @distributor sysname
  2871.     DECLARE @distribdb sysname
  2872.     DECLARE @distproc nvarchar (300)
  2873.     DECLARE @retcode int
  2874.     DECLARE @type nvarchar(10)
  2875.  
  2876.     SELECT @transpublishdb_bit = 1
  2877.     SELECT @mergepublishdb_bit = 4
  2878.  
  2879.  
  2880.     /*
  2881.     ** Parameter Check:  @subscriber.
  2882.     ** Check to make sure that the subscriber exists.
  2883.     */
  2884.  
  2885.     IF @subscriber IS NULL
  2886.         BEGIN
  2887.             RAISERROR (14043, 16, -1, '@subscriber')
  2888.             RETURN (1)
  2889.         END
  2890.  
  2891.     EXECUTE @retcode = dbo.sp_validname @subscriber
  2892.  
  2893.     IF @retcode <> 0
  2894.     RETURN (1)
  2895.  
  2896.     IF lower(@subscriber) <> 'all' and NOT EXISTS (SELECT *
  2897.                      FROM master..sysservers
  2898.                     WHERE UPPER(srvname) = UPPER(@subscriber) collate database_default
  2899.                       AND srvstatus & 4 <> 0)
  2900.  
  2901.         BEGIN
  2902.             RAISERROR (14048, 16, -1, @subscriber)
  2903.             RETURN (1)
  2904.         END
  2905.  
  2906.     if lower(@subscriber) = 'all'
  2907.     begin
  2908.         DECLARE hCdropsubscriber_all CURSOR LOCAL FAST_FORWARD FOR
  2909.             SELECT srvname
  2910.                      FROM master..sysservers
  2911.                     WHERE srvstatus & 4 <> 0
  2912.         FOR READ ONLY
  2913.         OPEN hCdropsubscriber_all
  2914.         FETCH hCdropsubscriber_all INTO @subscriber
  2915.     
  2916.         WHILE (@@fetch_status <> -1)
  2917.         BEGIN
  2918.             EXECUTE @retcode  = dbo.sp_dropsubscriber
  2919.                 @subscriber = @subscriber,    
  2920.                 @ignore_distributor = @ignore_distributor,
  2921.                 @reserved = @reserved
  2922.             IF @@ERROR <> 0 OR @retcode <> 0
  2923.             BEGIN
  2924.                 CLOSE hCdropsubscriber_all
  2925.                 DEALLOCATE hCdropsubscriber_all
  2926.                 RETURN (1)
  2927.             END
  2928.             FETCH hCdropsubscriber_all INTO @subscriber
  2929.         end
  2930.         
  2931.         CLOSE hCdropsubscriber_all
  2932.         DEALLOCATE hCdropsubscriber_all
  2933.  
  2934.         return(0)
  2935.     end
  2936.  
  2937.  
  2938.     /*
  2939.     ** There should be no subscription by the subscriber
  2940.     ** Open a cursor the published databases.
  2941.     **
  2942.     */
  2943.     DECLARE hCdropsubscriber CURSOR LOCAL FAST_FORWARD FOR
  2944.             SELECT name, N'tran' FROM master..sysdatabases      
  2945.                 WHERE (category & @transpublishdb_bit) <> 0 
  2946.         UNION
  2947.             select name, N'merge' from master..sysdatabases      
  2948.                 WHERE (category & @mergepublishdb_bit) <> 0 
  2949.     FOR READ ONLY
  2950.  
  2951.     OPEN hCdropsubscriber
  2952.     FETCH hCdropsubscriber INTO @db_name, @type
  2953.     
  2954.     WHILE (@@fetch_status <> -1)
  2955.     BEGIN        
  2956.         IF LOWER(@reserved collate SQL_Latin1_General_CP1_CS_AS) = 'drop_subscriptions'
  2957.         BEGIN
  2958.             if @type = 'tran'
  2959.             begin
  2960.                 SELECT @command = quotename(@db_name) + '.dbo.sp_dropsubscription'
  2961.                 EXECUTE @retcode  = @command 
  2962.                     @publication = 'all', 
  2963.                     @article = 'all', 
  2964.                     @subscriber = @subscriber,
  2965.                     @ignore_distributor = @ignore_distributor
  2966.             end
  2967.             if @type = 'merge'
  2968.             begin
  2969.                 SELECT @command = quotename(@db_name) + '.dbo.sp_dropmergesubscription'
  2970.                 EXECUTE @retcode  = @command 
  2971.                     @publication = 'all', 
  2972.                     @subscriber = @subscriber,
  2973.                     @subscription_type = 'both',
  2974.                     @ignore_distributor = @ignore_distributor
  2975.             end
  2976.             IF @@ERROR <> 0 OR @retcode <> 0
  2977.             BEGIN
  2978.                 CLOSE hCdropsubscriber
  2979.                 DEALLOCATE hCdropsubscriber
  2980.                 RETURN (1)
  2981.             END
  2982.         END
  2983.         
  2984.         -- Set @foundSubscription to not null so that sp_helpsubscription will not display
  2985.         -- result set
  2986.         select @foundSubscription = 0
  2987.  
  2988.         if @type = 'tran'
  2989.         begin
  2990.             SELECT @command = quotename(@db_name) + '.dbo.sp_helpsubscription'
  2991.             EXECUTE @retcode  = @command @publication = '%', @article = '%', 
  2992.                 @subscriber = @subscriber,
  2993.                 @found = @foundSubscription  OUTPUT
  2994.  
  2995.             IF @@ERROR <> 0 OR @retcode <> 0
  2996.             BEGIN
  2997.                 CLOSE hCdropsubscriber
  2998.                 DEALLOCATE hCdropsubscriber
  2999.                 RETURN (1)
  3000.             END
  3001.         end
  3002.         else
  3003.         begin
  3004.             SELECT @command = quotename(@db_name) + '.dbo.sp_helpmergesubscription'
  3005.             EXECUTE @retcode  = @command @publication = '%', 
  3006.                 @subscriber = @subscriber,
  3007.                 @subscription_type = 'both',
  3008.                 @found = @foundSubscription  OUTPUT
  3009.  
  3010.             IF @@ERROR <> 0 OR @retcode <> 0
  3011.             BEGIN
  3012.                 CLOSE hCdropsubscriber
  3013.                 DEALLOCATE hCdropsubscriber
  3014.                 RETURN (1)
  3015.             END
  3016.         end
  3017.  
  3018.  
  3019.         IF @foundSubscription <> 0
  3020.         BEGIN
  3021.             CLOSE hCdropsubscriber
  3022.             DEALLOCATE hCdropsubscriber
  3023.             RAISERROR ( 14144,  16, -1, @subscriber, @db_name)
  3024.             RETURN (1)
  3025.         END
  3026.  
  3027.         FETCH hCdropsubscriber INTO @db_name, @type
  3028.  
  3029.     END
  3030.     
  3031.     CLOSE hCdropsubscriber
  3032.     DEALLOCATE hCdropsubscriber
  3033.  
  3034.     /*
  3035.     ** Drop the subsubscriber_info in the distribution database
  3036.     */
  3037.     /*
  3038.     ** if @ignore_distributor = 1, we are in bruteforce cleanup mode, don't do RPC.
  3039.     */
  3040.     if @ignore_distributor = 0
  3041.     begin
  3042.  
  3043.  
  3044.         /*
  3045.         ** Get distribution server information for remote RPC
  3046.         ** agent verification.
  3047.         */
  3048.         EXEC @retcode = dbo.sp_helpdistributor 
  3049.             @rpcsrvname = @distributor OUTPUT,
  3050.             @distribdb = @distribdb OUTPUT
  3051.         IF @@error <> 0 OR @retcode <> 0
  3052.             BEGIN
  3053.             RAISERROR (14071, 16, -1)
  3054.                RETURN (1)
  3055.         END
  3056.         
  3057.         /*
  3058.         ** Insert information into MSsubscriber_info in the distribution db
  3059.         */
  3060.         if @distribdb is not null
  3061.         begin
  3062.             SELECT @distproc = RTRIM(@distributor) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSdrop_subscriber_info'
  3063.             EXEC @retcode = @distproc
  3064.                  @publisher = @@SERVERNAME,
  3065.                  @subscriber = @subscriber
  3066.  
  3067.             IF @@error <> 0 OR @retcode <> 0
  3068.                 BEGIN
  3069.                 RAISERROR (14042, 16, -1)
  3070.                 RETURN (1)
  3071.             END
  3072.         end
  3073.     end
  3074.  
  3075.     -- We have already validated the existence of the server at this point
  3076.     SELECT @subscriber = fn_getpersistedservernamecasevariation(@subscriber) collate database_default
  3077.  
  3078.     /*
  3079.     ** Turn off the subscriber server option.
  3080.     */
  3081.     EXECUTE @retcode = dbo.sp_serveroption @subscriber, 'sub', false
  3082.     IF @@ERROR <> 0 OR @retcode <> 0
  3083.     BEGIN
  3084.        RAISERROR (14047, 16, -1, @subscriber)
  3085.        RETURN (1)
  3086.     END
  3087.  
  3088.     RAISERROR (14062, 10, -1)
  3089. go
  3090.  
  3091. raiserror('Creating procedure sp_dsninfo', 0,1)
  3092. GO
  3093.  
  3094. CREATE PROCEDURE sp_dsninfo
  3095. --  xp_dsninfo does not support unicode
  3096.     @dsn varchar(128),
  3097.     @infotype varchar(128) = NULL,
  3098.     @login varchar(128) = NULL,
  3099.     @password varchar(128) = NULL,
  3100.     @dso_type int = 1  /* 1 is ODBC, 3 OLEDB. */
  3101.     AS
  3102.  
  3103.     SET NOCOUNT ON
  3104.  
  3105.     DECLARE @distributor sysname
  3106.     DECLARE @distproc nvarchar (300)
  3107.     DECLARE @retcode int
  3108.     DECLARE @dsotype_odbc int
  3109.     DECLARE @dsotype_oledb int
  3110.  
  3111.     select @dsotype_odbc = 1
  3112.     select @dsotype_oledb = 3
  3113.  
  3114.     /*
  3115.     ** Get distribution server information for remote RPC
  3116.     ** subscription calls.
  3117.     */
  3118.  
  3119.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT
  3120.     IF @@error <> 0 OR @retcode <> 0
  3121.         BEGIN
  3122.         RAISERROR (14071, 16, -1)
  3123.             RETURN (1)
  3124.     END
  3125.  
  3126.     if (@dso_type = @dsotype_odbc)
  3127.     begin
  3128.         /*
  3129.         ** Call xp_dsninfo
  3130.         */
  3131.         SELECT @distproc = RTRIM(@distributor) + '.master..xp_dsninfo'
  3132.         EXEC @retcode = @distproc @dsn, @infotype, @login, @password
  3133.         IF @@error <> 0
  3134.             BEGIN
  3135.             RAISERROR (14071, 16, -1)
  3136.             RETURN (1)
  3137.         END
  3138.     end
  3139.     else if (@dso_type = @dsotype_oledb)
  3140.     begin
  3141.         /*
  3142.         ** Call sp_oledbinfo
  3143.         */
  3144.         EXEC @retcode = master.dbo.sp_oledbinfo @dsn, @infotype, @login, @password
  3145.         IF @@error <> 0
  3146.             BEGIN
  3147.             RAISERROR (14071, 16, -1)
  3148.             RETURN (1)
  3149.         END
  3150.     end
  3151.  
  3152. go
  3153.  
  3154. dump tran master with no_log
  3155. go
  3156.  
  3157. raiserror('Creating procedure sp_publishdb', 0,1)
  3158. GO
  3159.  
  3160. /* For backward compatible */
  3161. CREATE PROCEDURE sp_publishdb @dbname sysname,@value nvarchar (5)
  3162.     AS
  3163.  
  3164.     DECLARE @retcode int
  3165.  
  3166.     EXECUTE @retcode = dbo.sp_replicationdboption @dbname, 'publish', @value
  3167.  
  3168.     IF @@ERROR <> 0 or @retcode <> 0
  3169.     BEGIN
  3170.        RETURN (1)
  3171.     END
  3172.  
  3173.     RETURN(0)
  3174. go
  3175.  
  3176.  
  3177. raiserror('Creating procedure sp_MScreate_dist_tables', 0,1)
  3178. GO
  3179.  
  3180. CREATE PROCEDURE sp_MScreate_dist_tables
  3181. AS
  3182.  
  3183.    /*
  3184.    ** Important:  
  3185.    ** We use varbinary(16) for xact_id and xact_seqno, we don't want ending nulls
  3186.    ** to be truncated by the server
  3187.    **
  3188.    ** Also, in MSrepl_commands, we don't want ending space to be truncated.  
  3189.    */
  3190.    SET ANSI_PADDING ON
  3191.  
  3192.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_version' and type = 'U')
  3193.    BEGIN
  3194.       /****************************************************************************/
  3195.       raiserror('Creating table MSrepl_version', 0,1)
  3196.       /****************************************************************************/
  3197.       CREATE TABLE dbo.MSrepl_version
  3198.       (
  3199.       major_version int NOT NULL,
  3200.       minor_version int NOT NULL,
  3201.       revision int NOT NULL,
  3202.       db_existed bit NULL
  3203.       )
  3204.       CREATE UNIQUE CLUSTERED INDEX ucMSrepl_version ON dbo.MSrepl_version
  3205.                     (major_version, minor_version, revision)
  3206.     
  3207.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_version'
  3208.       
  3209.       INSERT INTO MSrepl_version VALUES (7,0,0,0)
  3210.    END
  3211.  
  3212.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSpublisher_databases' and type = 'U')
  3213.    BEGIN
  3214.       /****************************************************************************/
  3215.       raiserror('Creating table MSpublisher_databases', 0,1)
  3216.       /****************************************************************************/
  3217.       CREATE TABLE dbo.MSpublisher_databases(
  3218.         publisher_id smallint NOT NULL,
  3219.         publisher_db sysname NULL,
  3220.         id int identity NOT NULL,                   
  3221.     )
  3222.       EXEC dbo.sp_MS_marksystemobject 'MSpublisher_databases'
  3223.  
  3224.       raiserror('Creating clustered index ucMSpublisher_databases', 0,1)
  3225.       CREATE UNIQUE CLUSTERED INDEX ucMSpublisher_databases ON dbo.MSpublisher_databases
  3226.      (publisher_id, publisher_db, id)
  3227.    END
  3228.  
  3229.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSpublications' and type = 'U')
  3230.    BEGIN
  3231.       /****************************************************************************/
  3232.       raiserror('Creating table MSpublications', 0,1)
  3233.       /****************************************************************************/
  3234.       CREATE TABLE dbo.MSpublications (
  3235.         publisher_id smallint NOT NULL,
  3236.         publisher_db sysname NULL,
  3237.         publication sysname NOT NULL,
  3238.         publication_id int identity NOT NULL,   -- This id IS NOT the same as the SQL Server publisher's
  3239.         publication_type int NOT NULL,          -- 0 = Snapshot 1 = Transactional
  3240.         thirdparty_flag bit NOT NULL,           -- 0 = SQL Server 1 = Third Party
  3241.         independent_agent bit NOT NULL,
  3242.         immediate_sync bit NOT NULL,
  3243.         allow_push bit NOT NULL,
  3244.         allow_pull bit NOT NULL,
  3245.         allow_anonymous bit NOT NULL,
  3246.         description nvarchar(255) NULL,
  3247.         vendor_name nvarchar(100) NULL,
  3248.         retention    int NULL,
  3249.         sync_method int default 0 NOT NULL,
  3250.         allow_subscription_copy bit default 0 not null,
  3251.         thirdparty_options int null,
  3252.         allow_queued_tran bit default 0    not null
  3253.         )
  3254.       EXEC dbo.sp_MS_marksystemobject 'MSpublications'
  3255.  
  3256.      -- publication_id needs to be the first columns in the index. It
  3257.      -- is used in sp_MSmaximum_cleanup_seqno.
  3258.       raiserror('Creating clustered index ucMSpublications', 0,1)
  3259.       CREATE UNIQUE CLUSTERED INDEX ucMSpublications ON dbo.MSpublications
  3260.      (publication_id,  publication, publisher_db, publisher_id)
  3261.  
  3262.    END
  3263.    ELSE
  3264.    BEGIN
  3265.       IF NOT EXISTS (select * from syscolumns
  3266.         where name = 'sync_method'
  3267.         and id=object_id('MSpublications'))
  3268.       BEGIN
  3269.          ALTER TABLE MSpublications ADD sync_method int default 0 not null
  3270.       END
  3271.  
  3272.       IF NOT EXISTS (select * from syscolumns
  3273.         where name = 'allow_subscription_copy'
  3274.         and id=object_id('MSpublications'))
  3275.       BEGIN
  3276.          ALTER TABLE MSpublications ADD allow_subscription_copy bit default 0 not null
  3277.       END
  3278.       IF NOT EXISTS (select * from syscolumns
  3279.         where name = 'thirdparty_options'
  3280.         and id=object_id('MSpublications'))
  3281.       BEGIN
  3282.          ALTER TABLE MSpublications ADD thirdparty_options int null
  3283.       END
  3284.  
  3285.       IF NOT EXISTS (select * from syscolumns
  3286.         where name = 'allow_queued_tran'
  3287.         and id=object_id('MSpublications'))
  3288.       BEGIN
  3289.          ALTER TABLE MSpublications ADD allow_queued_tran bit default 0    not null
  3290.       END
  3291.    END
  3292.  
  3293.  
  3294.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSarticles' and type = 'U')
  3295.    BEGIN
  3296.       /****************************************************************************/
  3297.       raiserror('Creating table MSarticles', 0,1)
  3298.       /****************************************************************************/
  3299.     CREATE TABLE dbo.MSarticles (
  3300.         publisher_id smallint NOT NULL,
  3301.         publisher_db sysname NULL,
  3302.         publication_id int NOT NULL,
  3303.         article sysname NOT NULL,
  3304.         article_id int NOT NULL,                            -- This id is the same as a SQL Server Publisher's
  3305.         destination_object sysname NULL,
  3306.         source_owner    sysname NULL,
  3307.         source_object sysname NULL,
  3308.         description nvarchar(255) NULL
  3309.     )   
  3310.  
  3311.       EXEC dbo.sp_MS_marksystemobject 'MSarticles'
  3312.  
  3313.       raiserror('Creating clustered index ucMSarticles', 0,1)
  3314.       CREATE UNIQUE CLUSTERED INDEX ucMSarticles ON dbo.MSarticles
  3315.      (publisher_db, publisher_id, article_id, article, publication_id)
  3316.    END
  3317.  
  3318.     IF NOT EXISTS( SELECT * FROM sysobjects WHERE name = 'MSsync_states' )
  3319.     BEGIN
  3320.         CREATE TABLE dbo.MSsync_states
  3321.         (
  3322.             publisher_id smallint not null,
  3323.             publisher_db sysname not null,
  3324.             publication_id int not null 
  3325.         )
  3326.         EXEC dbo.sp_MS_marksystemobject 'MSsync_states'
  3327.  
  3328.         CREATE UNIQUE CLUSTERED INDEX ucMSsyncstates on dbo.MSsync_states
  3329.         ( publisher_id, publisher_db, publication_id )
  3330.     END
  3331.  
  3332.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsubscriptions' and type = 'U')
  3333.    BEGIN
  3334.       /****************************************************************************/
  3335.       raiserror('Creating table MSsubscriptions', 0,1)
  3336.       /****************************************************************************/
  3337.        CREATE TABLE dbo.MSsubscriptions
  3338.       (
  3339.         publisher_database_id int NOT NULL,         -- Used to reference MSrepl_transactions and MSrepl_commands
  3340.         publisher_id smallint NOT NULL,
  3341.         publisher_db sysname NOT NULL,
  3342.         publication_id int NOT NULL,
  3343.         article_id int NOT NULL,
  3344.         subscriber_id smallint NOT NULL,
  3345.         subscriber_db sysname NOT NULL,
  3346.         subscription_type int NOT NULL,         -- 0 = push, 1 = pull, 2 = anonymous 
  3347.         sync_type tinyint NOT NULL,                 -- 1 = automatic 2 = no sync
  3348.         status tinyint NOT NULL,                        -- 0 = inactive, 1 = subscribed, 2 = active 
  3349.         subscription_seqno varbinary(16) NOT NULL,  -- publisher's database sequence number 
  3350.         snapshot_seqno_flag bit NOT NULL,               -- 1 if subscription_seqno is the snapshot seqno
  3351.         independent_agent bit NOT NULL,             -- Value carried over from MSpublications
  3352.         subscription_time datetime NOT NULL, 
  3353.         loopback_detection bit NOT NULL,  
  3354.         agent_id int NOT NULL,
  3355.         update_mode tinyint NOT NULL,                -- 0 = read only, 1 = sync tran, 2 = queued tran, 
  3356.                                                     -- 3 = failover, 4 = sqlqueued tran, 5 = sqlqueued failover
  3357.         publisher_seqno varbinary(16) NOT NULL,
  3358.         ss_cplt_seqno   varbinary(16) NOT NULL
  3359.       )
  3360.  
  3361.       EXEC dbo.sp_MS_marksystemobject 'MSsubscriptions'
  3362.  
  3363.       raiserror('Creating clustered index ucMSsubscirptions', 0,1)
  3364.       CREATE UNIQUE CLUSTERED INDEX ucMSsubscriptions ON dbo.MSsubscriptions
  3365.       (agent_id, article_id)
  3366.  
  3367.       -- Index used by sp_MSdelete_publisherdb_trans
  3368.       raiserror('Creating index iMSsubscriptions', 0,1)
  3369.       CREATE INDEX iMSsubscriptions ON dbo.MSsubscriptions
  3370.       (publisher_database_id, article_id, subscriber_id, subscriber_db, publication_id, publisher_db, publisher_id)
  3371.     END
  3372.     ELSE
  3373.     BEGIN
  3374.         if not exists ( select * from syscolumns 
  3375.                         where id = object_id( N'MSsubscriptions' ) 
  3376.                         and name = N'ss_cplt_seqno' )
  3377.         begin                
  3378.             exec ('alter table MSsubscriptions add ss_cplt_seqno varbinary(16) NULL')
  3379.             exec ('update MSsubscriptions set ss_cplt_seqno = publisher_seqno')
  3380.             exec ('alter table MSsubscriptions alter column ss_cplt_seqno varbinary(16) NOT NULL')
  3381.         end
  3382.     END
  3383.  
  3384.    -- For beta 3 upgrade, we need to create new index here.
  3385.    if not exists (select * from sysindexes where name = 'iMSsubscriptions2')
  3386.    begin
  3387.       -- Index used by sp_MSdelete_publisherdb_trans
  3388.       raiserror('Creating index iMSsubscriptions2', 0,1)
  3389.       CREATE INDEX iMSsubscriptions2 ON dbo.MSsubscriptions
  3390.       (publisher_database_id, subscription_seqno)
  3391.    end
  3392.  
  3393.    
  3394.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSmerge_subscriptions' and type = 'U')
  3395.    BEGIN
  3396.       /****************************************************************************/
  3397.       raiserror('Creating table MSmerge_subscriptions', 0,1)
  3398.       /****************************************************************************/
  3399.        CREATE TABLE dbo.MSmerge_subscriptions
  3400.       (
  3401.         publisher_id smallint NOT NULL,
  3402.         publisher_db sysname NULL,
  3403.         publication_id int NOT NULL,
  3404.         subscriber_id smallint NOT NULL,
  3405.         subscriber_db sysname NULL,
  3406.         subscription_type int NULL,         -- 0 = push, 1 = pull, 2 = anonymous 
  3407.         sync_type tinyint NOT NULL,                 -- 1 = automatic 2 = no sync
  3408.         status tinyint NOT NULL,                        -- 0 = inactive, 1 = subscribed, 2 = active 
  3409.         subscription_time datetime NOT NULL
  3410.       )
  3411.  
  3412.       EXEC dbo.sp_MS_marksystemobject 'MSmerge_subscriptions'
  3413.  
  3414.       raiserror('Creating clustered index ucMSmerge_subscriptions', 0,1)
  3415.       CREATE UNIQUE CLUSTERED INDEX ucMSmerge_subscriptions ON dbo.MSmerge_subscriptions
  3416.      (publisher_id, publisher_db, publication_id, subscriber_id, subscriber_db)
  3417.    END
  3418.  
  3419.  
  3420.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_transactions' and type = 'U')
  3421.    BEGIN
  3422.       /****************************************************************************/
  3423.       raiserror('Creating table MSrepl_transactions', 0,1)
  3424.       /****************************************************************************/
  3425.       CREATE TABLE dbo.MSrepl_transactions
  3426.       (
  3427.       publisher_database_id int NOT NULL,
  3428.       xact_id varbinary(16) NULL,
  3429.       xact_seqno varbinary (16 )  NOT NULL,
  3430.       entry_time datetime  NOT NULL
  3431.       )
  3432.  
  3433.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_transactions'
  3434.  
  3435.       raiserror('Creating clustered index usMSrepl_transactions', 0,1)
  3436.       CREATE UNIQUE CLUSTERED INDEX ucMSrepl_transactions ON dbo.MSrepl_transactions
  3437.          (publisher_database_id, xact_seqno)
  3438.          WITH STATISTICS_NORECOMPUTE
  3439.  
  3440.       CREATE STATISTICS stat_publisher_database_id
  3441.          ON MSrepl_transactions (publisher_database_id)
  3442.          WITH NORECOMPUTE
  3443.  
  3444.       CREATE STATISTICS stat_xact_id
  3445.          ON MSrepl_transactions (xact_id)
  3446.          WITH NORECOMPUTE
  3447.  
  3448.       CREATE STATISTICS stat_xact_seqno
  3449.          ON MSrepl_transactions (xact_seqno)
  3450.          WITH NORECOMPUTE
  3451.  
  3452.       CREATE STATISTICS stat_entry_time
  3453.          ON MSrepl_transactions (entry_time)
  3454.          WITH NORECOMPUTE
  3455.  
  3456.    END
  3457.  
  3458.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_commands' and type = 'U')
  3459.    BEGIN
  3460.  
  3461.       /****************************************************************************/
  3462.       raiserror('Creating table MSrepl_commands', 0,1)
  3463.       /****************************************************************************/
  3464.  
  3465.  
  3466.       CREATE TABLE dbo.MSrepl_commands (
  3467.       publisher_database_id int not null,
  3468.       xact_seqno varbinary(16) not null,
  3469.       type int not null, 
  3470.       article_id int not null,
  3471.       originator_id int not null,
  3472.       command_id int not null,
  3473.       partial_command bit not null,
  3474.       command varbinary(1024) NULL
  3475.       )
  3476.  
  3477.  
  3478.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_commands'
  3479.  
  3480.       raiserror('Creating clusterd index ucMSrepl_commands', 0,1)
  3481.       CREATE UNIQUE CLUSTERED INDEX ucMSrepl_commands ON dbo.MSrepl_commands
  3482.          (publisher_database_id, xact_seqno, command_id)
  3483.          WITH STATISTICS_NORECOMPUTE
  3484.  
  3485.       CREATE STATISTICS stat_xact_seqno
  3486.          ON MSrepl_commands (xact_seqno)
  3487.          WITH NORECOMPUTE
  3488.  
  3489.       CREATE STATISTICS stat_type
  3490.          ON MSrepl_commands (type)
  3491.          WITH NORECOMPUTE
  3492.  
  3493.       CREATE STATISTICS stat_article_id
  3494.          ON MSrepl_commands (article_id)
  3495.          WITH NORECOMPUTE
  3496.  
  3497.       CREATE STATISTICS stat_originator_id
  3498.          ON MSrepl_commands (originator_id)
  3499.          WITH NORECOMPUTE
  3500.  
  3501.       CREATE STATISTICS stat_command_id
  3502.          ON MSrepl_commands (command_id)
  3503.          WITH NORECOMPUTE
  3504.  
  3505.       CREATE STATISTICS stat_partial_command
  3506.          ON MSrepl_commands (partial_command)
  3507.          WITH NORECOMPUTE
  3508.  
  3509.    END
  3510.  
  3511.  
  3512.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_originators' and type = 'U')
  3513.    BEGIN
  3514.       /****************************************************************************/
  3515.       raiserror('Creating table MSrepl_orginators', 0,1)
  3516.       /****************************************************************************/
  3517.       CREATE TABLE dbo.MSrepl_originators
  3518.       (
  3519.       id int identity not null,
  3520.       publisher_database_id int not null,
  3521.       srvname   sysname not null,
  3522.       dbname    sysname not null
  3523.       )
  3524.  
  3525.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_originators'
  3526.  
  3527.       raiserror('Creating clustered index usMSrepl_originators', 0,1)
  3528.       CREATE UNIQUE CLUSTERED INDEX ucMSrepl_originators ON dbo.MSrepl_originators
  3529.          (publisher_database_id, srvname, dbname)
  3530.    END
  3531.  
  3532.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsubscriber_info' and type = 'U')
  3533.    BEGIN
  3534.       /****************************************************************************/
  3535.       raiserror('Creating table MSsubscriber_info', 0,1)
  3536.       /****************************************************************************/
  3537.       CREATE TABLE dbo.MSsubscriber_info
  3538.       (
  3539.       publisher  sysname NOT NULL,
  3540.       subscriber  sysname NOT NULL,
  3541.       type tinyint NOT NULL,           /* 0: MS SQL Server 1: ODBC Data Source */
  3542.       login sysname NULL,
  3543.       password nvarchar(524) NULL,
  3544.       description nvarchar(255) NULL,
  3545.       security_mode int NOT NULL
  3546.       )
  3547.  
  3548.       EXEC dbo.sp_MS_marksystemobject 'MSsubscriber_info'
  3549.  
  3550.       raiserror('Creating clustered index ucMSsubscriber_info', 0,1)
  3551.       CREATE UNIQUE CLUSTERED INDEX ucMSsubscriber_info ON dbo.MSsubscriber_info
  3552.      (publisher, subscriber)
  3553.    END
  3554.    ELSE
  3555.    BEGIN
  3556.       IF NOT EXISTS (select * from syscolumns
  3557.         where name = 'description'
  3558.         and id=object_id('MSsubscriber_info'))
  3559.       BEGIN
  3560.          
  3561.          ALTER TABLE MSsubscriber_info ADD description nvarchar (255) NULL
  3562.          UPDATE MSsubscriber_info SET description = 'SQL Server 6.0'
  3563.       END
  3564.    END
  3565.  
  3566.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsubscriber_schedule' and type = 'U')
  3567.    BEGIN
  3568.          raiserror('Creating table MSsubscriber_schedule', 0,1)
  3569.          
  3570.         CREATE TABLE dbo.MSsubscriber_schedule
  3571.         (
  3572.             publisher  sysname NOT NULL,
  3573.             subscriber  sysname NOT NULL,
  3574.             agent_type      smallint NOT NULL,   -- 0 for distribution agent, 1 for merge agent
  3575.             frequency_type int NOT NULL,
  3576.             frequency_interval int NOT NULL,
  3577.             frequency_relative_interval int NOT NULL,
  3578.             frequency_recurrence_factor int NOT NULL,
  3579.             frequency_subday int NOT NULL,
  3580.             frequency_subday_interval int NOT NULL,
  3581.             active_start_time_of_day int NOT NULL,
  3582.             active_end_time_of_day int NOT NULL,
  3583.             active_start_date int NOT NULL,
  3584.             active_end_date int NOT NULL
  3585.         )
  3586.       EXEC dbo.sp_MS_marksystemobject 'MSsubscriber_schedule'
  3587.  
  3588.         CREATE UNIQUE CLUSTERED INDEX ucMSsubscriber_schedule ON dbo.MSsubscriber_schedule
  3589.      (publisher, subscriber, agent_type)
  3590.      
  3591.     END
  3592.  
  3593.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsnapshot_history' and type = 'U')
  3594.    BEGIN
  3595.         /****************************************************************************/
  3596.          raiserror('Creating table MSsnapshot_history', 0,1)
  3597.         /****************************************************************************/
  3598.         CREATE TABLE dbo.MSsnapshot_history 
  3599.         (
  3600.         agent_id int NOT NULL,
  3601.         runstatus int NOT NULL,
  3602.         start_time datetime NOT NULL,        
  3603.         time datetime NOT NULL,    
  3604.         duration int NOT NULL,            
  3605.         comments nvarchar(255) NOT NULL,
  3606.  
  3607.         -- Session summary statistics
  3608.         delivered_transactions int NOT NULL,
  3609.         delivered_commands int NOT NULL,
  3610.         delivery_rate float NOT NULL,
  3611.  
  3612.         error_id int NOT NULL,
  3613.         timestamp NOT NULL
  3614.     )
  3615.  
  3616.       EXEC dbo.sp_MS_marksystemobject 'MSsnapshot_history'
  3617.  
  3618.      raiserror('Creating clustered index ucMSsnapshot_history', 0,1)
  3619.     CREATE UNIQUE CLUSTERED INDEX ucMSsnapshot_history ON dbo.MSsnapshot_history
  3620.         (agent_id, timestamp, start_time, time)
  3621.  
  3622.     if not exists (select * from sysindexes where name = 'nc1MSsnapshot_history')
  3623.     begin
  3624.          raiserror('Creating clustered index nc1MSsnapshot_history', 0,1)
  3625.         create nonclustered index nc1MSsnapshot_history on MSsnapshot_history(agent_id, start_time) 
  3626.     end    
  3627.  
  3628.    END
  3629.        
  3630.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSlogreader_history' and type = 'U')
  3631.    BEGIN
  3632.         /****************************************************************************/
  3633.         raiserror('Creating table MSlogreader_history', 0,1)
  3634.         /****************************************************************************/    
  3635.         CREATE TABLE dbo.MSlogreader_history
  3636.         (
  3637.         agent_id int NOT NULL,
  3638.         runstatus int NOT NULL,
  3639.         start_time datetime NOT NULL,
  3640.         time datetime NOT NULL,
  3641.         duration int NOT NULL,
  3642.         comments nvarchar(255) NOT NULL,
  3643.         xact_seqno varbinary(16) NULL,
  3644.  
  3645.         -- Session summary statistics
  3646.         delivery_time int NOT NULL,
  3647.         delivered_transactions int NOT NULL,
  3648.         delivered_commands int NOT NULL,
  3649.         average_commands int NOT NULL,
  3650.         delivery_rate float NOT NULL,
  3651.         delivery_latency int NOT NULL,
  3652.  
  3653.         error_id int NOT NULL,
  3654.         timestamp NOT NULL
  3655.         )
  3656.  
  3657.       EXEC dbo.sp_MS_marksystemobject 'MSlogreader_history'
  3658.  
  3659.     raiserror('Creating clustered index ucMSlogreader_history', 0,1)
  3660.     CREATE UNIQUE CLUSTERED INDEX ucMSlogreader_history ON dbo.MSlogreader_history
  3661.         (agent_id, timestamp, runstatus, start_time, time) 
  3662.  
  3663.     if not exists (select * from sysindexes where name = 'nc1MSlogreader_history')
  3664.     begin
  3665.         raiserror('Creating nonclustered index nc1MSlogreader_history', 0,1)
  3666.         create nonclustered index nc1MSlogreader_history on MSlogreader_history(agent_id, start_time) 
  3667.     end    
  3668.  
  3669.    END
  3670.  
  3671.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSdistribution_history' and type = 'U')
  3672.    BEGIN
  3673.         /****************************************************************************/
  3674.           raiserror('Creating table MSdistribution_history', 0,1)
  3675.         /****************************************************************************/    
  3676.  
  3677.         CREATE TABLE dbo.MSdistribution_history
  3678.         (
  3679.         agent_id int NOT NULL,
  3680.         runstatus int NOT NULL,
  3681.         start_time datetime NOT NULL,
  3682.         time datetime NOT NULL,
  3683.         duration int NOT NULL,
  3684.         comments nvarchar(255) NOT NULL,
  3685.         xact_seqno varbinary(16) NULL,
  3686.  
  3687.         -- Current statistics
  3688.         current_delivery_rate float NOT NULL,
  3689.         current_delivery_latency int NOT NULL,
  3690.  
  3691.         -- Session summary statistics
  3692.         delivered_transactions int NOT NULL,
  3693.         delivered_commands int NOT NULL,
  3694.         average_commands int NOT NULL,
  3695.         delivery_rate float NOT NULL,       
  3696.         delivery_latency int NOT NULL, 
  3697.  
  3698.         -- Summary statistics across all sessions
  3699.         total_delivered_commands int NOT NULL, 
  3700.  
  3701.         error_id int NOT NULL,
  3702.         updateable_row bit NOT NULL,
  3703.         timestamp NOT NULL
  3704.         )
  3705.  
  3706.     EXEC dbo.sp_MS_marksystemobject 'MSdistribution_history'
  3707.  
  3708.     raiserror('Creating clustered index ucMSdistribution_history', 0,1)
  3709.     CREATE UNIQUE CLUSTERED INDEX ucMSdistribution_history ON dbo.MSdistribution_history
  3710.         (agent_id, timestamp, runstatus, start_time, time) 
  3711.  
  3712.  
  3713.     if not exists (select * from sysindexes where name = 'nc1MSdistribution_history')
  3714.     begin
  3715.         raiserror('Creating clustered index nc1MSdistribution_history', 0,1)
  3716.         create nonclustered index nc1MSdistribution_history on MSdistribution_history(agent_id, start_time) 
  3717.     end    
  3718.  
  3719.    END
  3720.  
  3721.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSmerge_history' and type = 'U')
  3722.    BEGIN
  3723.         /****************************************************************************/
  3724.       raiserror('Creating table MSmerge_history', 0,1)
  3725.         /****************************************************************************/    
  3726.  
  3727.         CREATE TABLE dbo.MSmerge_history
  3728.         (
  3729.         agent_id                    int             NOT NULL,
  3730.         runstatus               int             NOT NULL,
  3731.         start_time              datetime        NOT NULL,
  3732.         time                    datetime        NOT NULL,
  3733.         duration                int             NOT NULL,
  3734.         comments                nvarchar(255)   NOT NULL,
  3735.         delivery_time               int         NOT NULL,
  3736.         delivery_rate               float       NOT NULL,
  3737.         publisher_insertcount       int         NULL,
  3738.         publisher_updatecount       int         NULL,
  3739.         publisher_deletecount       int         NULL,
  3740.         publisher_conflictcount     int         NULL,
  3741.         subscriber_insertcount      int         NULL,
  3742.         subscriber_updatecount      int         NULL,
  3743.         subscriber_deletecount      int         NULL,
  3744.         subscriber_conflictcount    int         NULL,
  3745.         error_id                    int         NOT NULL,
  3746.         timestamp                               NOT NULL,
  3747.         updateable_row                bit            NOT NULL
  3748.         )        
  3749.  
  3750.     EXEC dbo.sp_MS_marksystemobject 'MSmerge_history'
  3751.  
  3752.     raiserror('Creating clustered index ucMSmerge_history', 0,1)
  3753.     CREATE UNIQUE CLUSTERED INDEX ucMSmerge_history ON dbo.MSmerge_history
  3754.         (agent_id, timestamp, runstatus) 
  3755.         
  3756.     if not exists (select * from sysindexes where name = 'nc1MSmerge_history')
  3757.         begin
  3758.             raiserror('Creating nonclustered index nc1MSmerge_history', 0,1)
  3759.             create nonclustered index nc1MSmerge_history on MSmerge_history(agent_id, runstatus, start_time) 
  3760.         end
  3761.    END
  3762.    ELSE
  3763.    BEGIN
  3764.       IF NOT EXISTS (select * from syscolumns
  3765.         where name = 'updateable_row'
  3766.         and id=object_id('MSmerge_history'))
  3767.       BEGIN 
  3768.          ALTER TABLE MSmerge_history ADD updateable_row bit default 1 NOT NULL
  3769.       END
  3770.  
  3771.       if not exists (select * from sysindexes where name = 'nc1MSmerge_history' and id=object_id('MSmerge_history'))
  3772.       begin
  3773.             raiserror('Creating nonclustered index nc1MSmerge_history', 0,1)
  3774.             create nonclustered index nc1MSmerge_history on MSmerge_history(agent_id, runstatus, start_time) 
  3775.       end    
  3776.    END
  3777.  
  3778.  
  3779.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_errors' and type = 'U')
  3780.    BEGIN
  3781.         /****************************************************************************/
  3782.         raiserror('Creating table MSrepl_errors', 0,1)
  3783.         /****************************************************************************/    
  3784.  
  3785.         CREATE TABLE dbo.MSrepl_errors
  3786.         (
  3787.         id                  int NOT NULL,
  3788.         time                datetime NOT NULL,
  3789.         error_type_id       int NULL,
  3790.         source_type_id      int NULL,
  3791.         source_name         nvarchar(100) NULL,
  3792.         error_code          sysname NULL,
  3793.         error_text          ntext NULL,
  3794.         xact_seqno            varbinary(16) NULL,
  3795.         command_id            int NULL
  3796.         )
  3797.  
  3798.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_errors'
  3799.  
  3800.     raiserror('Creating clustered index ucMSrepl_errors', 0,1)
  3801.     CREATE CLUSTERED INDEX ucMSrepl_errors ON dbo.MSrepl_errors
  3802.         (id, time) -- WITH ALLOW_DUP_ROW
  3803.    END
  3804.    ELSE
  3805.    BEGIN
  3806.       IF NOT EXISTS (select * from syscolumns
  3807.         where name = 'xact_seqno'
  3808.         and id=object_id('MSrepl_errors'))
  3809.          ALTER TABLE MSrepl_errors ADD xact_seqno varbinary(16) NULL
  3810.  
  3811.       IF NOT EXISTS (select * from syscolumns
  3812.         where name = 'command_id'
  3813.         and id=object_id('MSrepl_errors'))
  3814.          ALTER TABLE MSrepl_errors ADD command_id int NULL
  3815.    END
  3816.  
  3817.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsnapshot_agents' and type = 'U')
  3818.    BEGIN
  3819.         /****************************************************************************/
  3820.         raiserror('Creating table MSsnapshot_agents', 0,1)
  3821.         /****************************************************************************/    
  3822.  
  3823.         CREATE TABLE dbo.MSsnapshot_agents
  3824.         (
  3825.         id                  int IDENTITY NOT NULL,
  3826.         name                nvarchar(100) NOT NULL,
  3827.         publisher_id        smallint NOT NULL,
  3828.         publisher_db        sysname NOT NULL,
  3829.         publication         sysname NOT NULL,
  3830.         publication_type    int NOT NULL,       -- 0 transactional 1 snapshot 2 merge
  3831.         local_job           bit NOT NULL,
  3832.         job_id              binary(16) NULL,
  3833.         profile_id  int     NOT NULL
  3834.         )
  3835.  
  3836.       EXEC dbo.sp_MS_marksystemobject 'MSsnapshot_agents'
  3837.  
  3838.     raiserror('Creating clustered index ucMSsnapshot_agents', 0,1)
  3839.     CREATE CLUSTERED INDEX ucMSsnapshot_agents ON dbo.MSsnapshot_agents
  3840.         (publication, publisher_db, publisher_id) 
  3841.  
  3842.     raiserror('Creatingindex iMSsnapshot_agents', 0,1)
  3843.     CREATE UNIQUE INDEX iMSsnapshot_agents ON dbo.MSsnapshot_agents
  3844.         (id)
  3845.    END
  3846.  
  3847.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSlogreader_agents' and type = 'U')
  3848.    BEGIN
  3849.         /****************************************************************************/
  3850.         raiserror('Creating table MSlogreader_agents', 0,1)
  3851.         /****************************************************************************/    
  3852.  
  3853.         CREATE TABLE dbo.MSlogreader_agents
  3854.         (
  3855.         id                  int IDENTITY NOT NULL,
  3856.         name                nvarchar(100) NOT NULL,
  3857.         publisher_id        smallint NOT NULL,
  3858.         publisher_db        sysname NOT NULL,
  3859.         publication         sysname NOT NULL,-- Not used for SQL Server publisher
  3860.         local_job           bit NOT NULL,
  3861.         job_id              binary(16) NULL,
  3862.         profile_id  int     NOT NULL
  3863.         )
  3864.  
  3865.       EXEC dbo.sp_MS_marksystemobject 'MSlogreader_agents'
  3866.  
  3867.     raiserror('Creating clustered index ucMSlogreader_agents', 0,1)
  3868.     CREATE CLUSTERED INDEX ucMSlogreader_agents ON dbo.MSlogreader_agents
  3869.         (publisher_db, publisher_id) 
  3870.  
  3871.     raiserror('Creatingindex iMSlogreader_agents', 0,1)
  3872.     CREATE UNIQUE INDEX iMSlogreader_agents ON dbo.MSlogreader_agents
  3873.         (id)
  3874.    END
  3875.  
  3876.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSdistribution_agents' and type = 'U')
  3877.    BEGIN
  3878.         /****************************************************************************/
  3879.         raiserror('Creating table MSdistribution_agents', 0,1)
  3880.         /****************************************************************************/    
  3881.  
  3882.         CREATE TABLE dbo.MSdistribution_agents
  3883.         (
  3884.         id                  int IDENTITY NOT NULL,
  3885.         name                nvarchar(100) NOT NULL,
  3886.         publisher_database_id int NOT NULL,
  3887.         publisher_id        smallint NOT NULL,
  3888.         publisher_db        sysname NOT NULL,
  3889.         publication         sysname NOT NULL,
  3890.         subscriber_id       smallint NULL, 
  3891.         subscriber_db       sysname NULL,
  3892.         subscription_type   int NOT NULL,
  3893.         local_job           bit NULL,
  3894.         job_id              binary(16) NULL,
  3895.         subscription_guid   binary(16) NOT NULL,
  3896.         profile_id  int     NOT NULL,
  3897.         anonymous_subid     uniqueidentifier NULL,
  3898.         subscriber_name     sysname NULL,
  3899.         virtual_agent_id    int NULL,
  3900.         anonymous_agent_id  int NULL,
  3901.         creation_date        datetime default (getdate()) not NULL,
  3902.         queue_id            sysname null,
  3903.         queue_status        int default 0 not null,
  3904.         offload_enabled     bit default 0 NOT NULL,
  3905.         offload_server      sysname NULL,
  3906.         dts_package_name    sysname NULL,
  3907.         dts_package_password nvarchar(524) NULL,
  3908.         dts_package_location int default 0 not null,
  3909.         sid                    varbinary(85) default suser_sid() not null,
  3910.         queue_server        sysname NULL
  3911.         )
  3912.  
  3913.         EXEC dbo.sp_MS_marksystemobject 'MSdistribution_agents'
  3914.  
  3915.         raiserror('Creating clustered index ucMSdistribution_agents', 0,1)
  3916.         CREATE UNIQUE CLUSTERED INDEX ucMSdistribution_agents ON dbo.MSdistribution_agents
  3917.             (id)
  3918.  
  3919.         raiserror('Creatingindex iMSdistribution_agents', 0,1)
  3920.         CREATE INDEX iMSdistribution_agents ON dbo.MSdistribution_agents
  3921.             (publication, publisher_db, publisher_id, subscriber_id, subscriber_db, anonymous_subid) 
  3922.    END
  3923.  
  3924.     if not exists (select * from syscolumns where 
  3925.         id = object_id('MSdistribution_agents') and
  3926.         name = 'creation_date')
  3927.     begin
  3928.         alter table MSdistribution_agents add creation_date datetime default (getdate()) not null
  3929.     end 
  3930.  
  3931.     if not exists (select * from syscolumns where 
  3932.         id = object_id('MSdistribution_agents') and
  3933.         name = 'offload_enabled')
  3934.     begin
  3935.         alter table MSdistribution_agents add offload_enabled bit default 0 not null
  3936.     end 
  3937.  
  3938.     if not exists (select * from syscolumns where 
  3939.         id = object_id('MSdistribution_agents') and
  3940.         name = 'offload_server')
  3941.     begin
  3942.         alter table MSdistribution_agents add offload_server sysname null
  3943.     end 
  3944.  
  3945.     if not exists (select * from syscolumns where 
  3946.         id = object_id('MSdistribution_agents') and
  3947.         name = 'queue_id')
  3948.     begin
  3949.         alter table MSdistribution_agents add queue_id sysname null
  3950.     end 
  3951.  
  3952.     if not exists (select * from syscolumns where 
  3953.         id = object_id('MSdistribution_agents') and
  3954.         name = 'queue_status')
  3955.     begin
  3956.         alter table MSdistribution_agents add queue_status int default 0 not null
  3957.     end 
  3958.  
  3959.     if not exists (select * from syscolumns where 
  3960.         id = object_id('MSdistribution_agents') and
  3961.         name = 'dts_package_name')
  3962.     begin
  3963.         alter table MSdistribution_agents add dts_package_name sysname null
  3964.     end 
  3965.     if not exists (select * from syscolumns where 
  3966.         id = object_id('MSdistribution_agents') and
  3967.         name = 'dts_package_password')
  3968.     begin
  3969.         alter table MSdistribution_agents add dts_package_password nvarchar(524) null
  3970.     end 
  3971.  
  3972.     if not exists (select * from syscolumns where 
  3973.         id = object_id('MSdistribution_agents') and
  3974.         name = 'dts_package_location')
  3975.     begin
  3976.         alter table MSdistribution_agents add dts_package_location int default 0 not null
  3977.     end 
  3978.  
  3979.     if not exists (select * from syscolumns where 
  3980.         id = object_id('MSdistribution_agents') and
  3981.         name = 'sid')
  3982.     begin
  3983.         -- set sid to be the upgrade user. db_owner or sysadmin
  3984.         -- can drop the agent entry
  3985.         alter table MSdistribution_agents add sid varbinary(85) default suser_sid() not null
  3986.     end 
  3987.  
  3988.     if not exists (select * from syscolumns where 
  3989.         id = object_id('MSdistribution_agents') and
  3990.         name = 'queue_server')
  3991.     begin
  3992.         alter table MSdistribution_agents add queue_server sysname null
  3993.         EXEC dbo.sp_MSupdate_mqserver_distdb
  3994.     end 
  3995.     
  3996.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSmerge_agents' and type = 'U')
  3997.    BEGIN
  3998.         /****************************************************************************/
  3999.         raiserror('Creating table MSmerge_agents', 0,1)
  4000.         /****************************************************************************/    
  4001.  
  4002.         CREATE TABLE dbo.MSmerge_agents
  4003.         (
  4004.         id                  int IDENTITY NOT NULL,
  4005.         name                nvarchar(100) NOT NULL,
  4006.         publisher_id        smallint NOT NULL,
  4007.         publisher_db        sysname NOT NULL,
  4008.         publication         sysname NOT NULL,
  4009.         subscriber_id       smallint NULL,
  4010.         subscriber_db       sysname NULL,
  4011.         local_job           bit NULL,
  4012.         job_id              binary(16) NULL,
  4013.         profile_id          int NULL,
  4014.         anonymous_subid     uniqueidentifier NULL,
  4015.         subscriber_name     sysname NULL,
  4016.         creation_date        datetime default (getdate()) not NULL,
  4017.         offload_enabled     bit default 0 NOT NULL,
  4018.         offload_server      sysname NULL,
  4019.         sid                    varbinary(85) default suser_sid() not null
  4020.         )
  4021.  
  4022.       EXEC dbo.sp_MS_marksystemobject 'MSmerge_agents'
  4023.  
  4024.     raiserror('Creating clustered index ucMSmerge_agents', 0,1)
  4025.     CREATE CLUSTERED INDEX ucMSmerge_agents ON dbo.MSmerge_agents
  4026.         (publication, publisher_db, publisher_id, subscriber_id, subscriber_db, anonymous_subid) 
  4027.     
  4028.     raiserror('Creatingindex iMSmerge_agents', 0,1)
  4029.     CREATE UNIQUE INDEX iMSmerge_agents ON dbo.MSmerge_agents
  4030.         (id)
  4031.     END
  4032.  
  4033.  
  4034.     -- Need to re_visit the indexing of this table
  4035.     if not exists (select * from sysobjects where name = 'MSrepl_identity_range')
  4036.         begin        
  4037.             raiserror('Creating table MSrepl_identity_range',0,1)
  4038.             
  4039.             create table dbo.MSrepl_identity_range (
  4040.             publisher                sysname not NULL, 
  4041.             publisher_db            sysname    not NULL,
  4042.             tablename                sysname not NULL,
  4043.             identity_support        int NULL,
  4044.             next_seed                bigint NULL, --resource control
  4045.             pub_range                bigint NULL, --publisher range
  4046.             range                    bigint NULL, -- set by sp_addmergearticle
  4047.             max_identity            bigint NULL, --resource control
  4048.             threshold                int    NULL,    --in percentage, set by sp_addmergearticle
  4049.             current_max                bigint NULL    --max value for current check constraint,set by sp_addmergearticle
  4050.             )
  4051.             exec dbo.sp_MS_marksystemobject MSrepl_identity_range
  4052.  
  4053.             grant select on MSrepl_identity_range to public
  4054.         end
  4055.  
  4056.     if not exists (select * from syscolumns where 
  4057.         id = object_id('MSmerge_agents') and
  4058.         name = 'creation_date')
  4059.     begin
  4060.         alter table MSmerge_agents add creation_date datetime default (getdate()) not null
  4061.     end 
  4062.  
  4063.     if not exists (select * from syscolumns where 
  4064.         id = object_id('MSmerge_agents') and
  4065.         name = 'offload_enabled')
  4066.     begin
  4067.         alter table MSmerge_agents add offload_enabled bit default 0 not null
  4068.     end 
  4069.  
  4070.     if not exists (select * from syscolumns where 
  4071.         id = object_id('MSmerge_agents') and
  4072.         name = 'offload_server')
  4073.     begin
  4074.         alter table MSmerge_agents add offload_server sysname null
  4075.     end 
  4076.  
  4077.     if not exists (select * from syscolumns where 
  4078.         id = object_id('MSmerge_agents') and
  4079.         name = 'sid')
  4080.     begin
  4081.         -- set sid to be the upgrade user. db_owner or sysadmin
  4082.         -- can drop the agent entry
  4083.         alter table MSmerge_agents add sid varbinary(85) default suser_sid() not null
  4084.     end 
  4085.  
  4086.     IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSpublication_access' and type = 'U')
  4087.     BEGIN
  4088.         /****************************************************************************/
  4089.         raiserror('Creating table MSpublication_access', 0,1)
  4090.         /****************************************************************************/    
  4091.  
  4092.         CREATE TABLE dbo.MSpublication_access
  4093.         (
  4094.         publication_id      int NULL,       -- Publication_id is unique in distribution database.
  4095.         login               sysname NOT NULL
  4096.                                             -- Logins in the publication access list, they nust
  4097.                                             -- exist at both publisher and distributor side.            
  4098.         )
  4099.  
  4100.         EXEC dbo.sp_MS_marksystemobject 'MSpublication_access'
  4101.  
  4102.         raiserror('Creating clustered index ucMSpublication_access', 0,1)
  4103.         CREATE UNIQUE CLUSTERED INDEX ucMSpublication_access ON dbo.MSpublication_access
  4104.             (publication_id, login) 
  4105.     END
  4106.  
  4107.     -- For b3 upgrade
  4108.     if NOT EXISTS (select * from syscolumns where name='retention' and id=object_id('MSpublications'))
  4109.     begin
  4110.         alter table MSpublications add retention int NULL 
  4111.         UPDATE msdb..MSdistributiondbs set max_distretention=72 where
  4112.             name = db_name() collate database_default
  4113.     end
  4114.  
  4115.     -- drop default_access column
  4116.     if exists (select * from syscolumns where id = object_id('MSpublications') and
  4117.         name = 'default_access')
  4118.     begin
  4119.         alter table MSpublications drop column default_access
  4120.     end 
  4121.  
  4122.     -- Drop publisher_id column
  4123.     if EXISTS (select * from syscolumns where name='publisher_id' and id=object_id('MSpublication_access'))
  4124.     begin
  4125.         drop index MSpublication_access.ucMSpublication_access
  4126.  
  4127.         alter table MSpublication_access drop column publisher_id
  4128.  
  4129.         raiserror('Creating clustered index ucMSpublication_access', 0,1)
  4130.         CREATE CLUSTERED INDEX ucMSpublication_access ON dbo.MSpublication_access
  4131.         (publication_id, login) 
  4132.     end
  4133.  
  4134.     IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSqreader_agents' and type = 'U')
  4135.     BEGIN
  4136.         /****************************************************************************/
  4137.         raiserror('Creating table MSqreader_agents', 0,1)
  4138.         /****************************************************************************/    
  4139.  
  4140.         CREATE TABLE dbo.MSqreader_agents
  4141.         (
  4142.         id                  int IDENTITY NOT NULL,
  4143.         name                nvarchar(100) NULL,
  4144.         job_id              binary(16) NULL,
  4145.         profile_id          int NULL
  4146.         )
  4147.  
  4148.       EXEC dbo.sp_MS_marksystemobject 'MSqreader_agents'
  4149.  
  4150.     raiserror('Creating unique index ucMSqreader_agents', 0,1)
  4151.     CREATE UNIQUE INDEX ucMSqreader_agents ON dbo.MSqreader_agents
  4152.         (id)
  4153.    END
  4154.  
  4155.     -- add columns for existing table
  4156.     if not exists (select * from syscolumns where 
  4157.         id = object_id('MSqreader_agents') and
  4158.         name = 'profile_id')
  4159.     begin
  4160.         alter table dbo.MSqreader_agents add profile_id int NULL
  4161.     end 
  4162.    
  4163.  
  4164.     IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSqreader_history' and type = 'U')
  4165.     BEGIN
  4166.     /****************************************************************************/
  4167.     raiserror('Creating table MSqreader_history', 0,1)
  4168.     /****************************************************************************/    
  4169.  
  4170.     CREATE TABLE dbo.MSqreader_history
  4171.     (
  4172.     agent_id int NOT NULL,
  4173.     publication_id int NULL,
  4174.     runstatus int NOT NULL,
  4175.     start_time datetime NOT NULL,
  4176.     time datetime NOT NULL,
  4177.     duration int NOT NULL,
  4178.     comments nvarchar(255) NOT NULL,
  4179.     transaction_id nvarchar(40) NULL, 
  4180.     transaction_status int NULL,
  4181.     transactions_processed int NULL DEFAULT 0,
  4182.     commands_processed int NULL DEFAULT 0,
  4183.     delivery_rate float NOT NULL DEFAULT 0.0,
  4184.     transaction_rate float NOT NULL DEFAULT 0.0,
  4185.     subscriber sysname NULL,
  4186.     subscriberdb sysname NULL,
  4187.     error_id int NULL,    
  4188.     timestamp NOT NULL
  4189.     )
  4190.  
  4191.     EXEC dbo.sp_MS_marksystemobject 'MSqreader_history'
  4192.  
  4193.     raiserror('Creating clustered index ucMSqreader_history', 0,1)
  4194.     CREATE CLUSTERED INDEX ucMSqreader_history ON dbo.MSqreader_history
  4195.     (agent_id, timestamp, runstatus, start_time, time) 
  4196.     END
  4197.  
  4198.     -- alter column publication_id
  4199.     if not exists (select * from syscolumns where 
  4200.         id = object_id('MSqreader_history') and
  4201.         name = 'publication_id')
  4202.     begin
  4203.         alter table dbo.MSqreader_history alter column publication_id int NULL
  4204.     end
  4205.  
  4206.     -- add columns for existing table
  4207.     if not exists (select * from syscolumns where 
  4208.         id = object_id('MSqreader_history') and
  4209.         name = 'error_id')
  4210.     begin
  4211.         alter table dbo.MSqreader_history add error_id int NULL
  4212.     end 
  4213.  
  4214.     if not exists (select * from syscolumns where 
  4215.         id = object_id('MSqreader_history') and
  4216.         name = 'transactions_processed')
  4217.     begin
  4218.         alter table dbo.MSqreader_history add transactions_processed int NULL DEFAULT 0
  4219.     end 
  4220.  
  4221.     if not exists (select * from syscolumns where 
  4222.         id = object_id('MSqreader_history') and
  4223.         name = 'delivery_rate')
  4224.     begin
  4225.         alter table dbo.MSqreader_history add delivery_rate float NOT NULL DEFAULT 0.0
  4226.     end 
  4227.  
  4228.     if not exists (select * from syscolumns where 
  4229.         id = object_id('MSqreader_history') and
  4230.         name = 'transaction_rate')
  4231.     begin
  4232.         alter table dbo.MSqreader_history add transaction_rate float NOT NULL DEFAULT 0.0
  4233.     end 
  4234.  
  4235.     IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_backup_lsns' and type = 'U')
  4236.     BEGIN
  4237.         /****************************************************************************/
  4238.         raiserror('Creating table MSrepl_backup_lsns', 0,1)
  4239.         /****************************************************************************/    
  4240.  
  4241.         CREATE TABLE dbo.MSrepl_backup_lsns
  4242.         (
  4243.         publisher_database_id int NOT NULL,
  4244.         valid_xact_id varbinary(16) NULL,
  4245.         valid_xact_seqno varbinary (16 ) NULL,
  4246.         next_xact_id varbinary(16) NULL,
  4247.         next_xact_seqno varbinary (16 )  NULL
  4248.         )
  4249.  
  4250.         EXEC dbo.sp_MS_marksystemobject 'MSrepl_backup_lsns'
  4251.  
  4252.         raiserror('Creating clustered index ucMSrepl_backup_lsns', 0,1)
  4253.         CREATE UNIQUE CLUSTERED INDEX ucMSrepl_backup_lsns ON dbo.MSrepl_backup_lsns
  4254.             (publisher_database_id)
  4255.  
  4256.         if exists (select * from MSpublisher_databases)
  4257.         begin
  4258.             /****************************************************************************/
  4259.             raiserror('Upgrading MSrepl_backup_lsns', 0,1)
  4260.             /****************************************************************************/    
  4261.             insert into MSrepl_backup_lsns select d.id, 
  4262.                 NULL, NULL, NULL, NULL from MSpublisher_databases d
  4263.             -- No need to set the lsns in the table since the 'sync with backup' option is 
  4264.             -- new in 8.0
  4265.         end
  4266.  
  4267.     END
  4268. GO
  4269.  
  4270. dump tran master with no_log
  4271. go
  4272.  
  4273. --
  4274. -- sp_MSupdate_mqserver_distdb
  4275. --
  4276. -- This procedure is called to upgrade the MSMQ related entries in 
  4277. -- MSdistribution_agents in distribution database - this processing 
  4278. -- cannot be done directly inside sp_MScreate_dist_tables or sp_vupgrade_distdb
  4279. -- since we need the queue_server column for the processing and we can
  4280. -- get a syntax error if the table exists without the column
  4281. --
  4282. -- Assumption : MSsubscription_agents has been created 
  4283. --                and column queue_server exists
  4284. --
  4285. raiserror('Creating procedure sp_MSupdate_mqserver_distdb', 0,1)
  4286. go
  4287. create procedure sp_MSupdate_mqserver_distdb
  4288. as
  4289. begin
  4290.     if exists (select * from MSdistribution_agents
  4291.         where queue_id IS NOT NULL and substring(queue_id, 1, 10) != N'mssqlqueue' and queue_server IS NULL)
  4292.     begin
  4293.         --
  4294.         -- we have entries for active Queued subscriptions using MSMQ that need upgrade
  4295.         --
  4296.         declare @queue_server sysname
  4297.  
  4298.         --
  4299.         -- prepare the queue server name
  4300.         --
  4301.         select @queue_server = @@servername
  4302.         if (charindex(N'\', @queue_server) > 0)
  4303.             select @queue_server = substring(@queue_server, 1, charindex(N'\', @queue_server) - 1)
  4304.  
  4305.         --
  4306.         -- update the queue_server column for these entries
  4307.         --
  4308.         update MSdistribution_agents
  4309.         set queue_server = @queue_server
  4310.         where queue_id IS NOT NULL and substring(queue_id, 1, 10) != N'mssqlqueue' and queue_server IS NULL
  4311.     end
  4312. end
  4313. go
  4314.  
  4315. raiserror('Creating procedure sp_MSadd_distributor_alerts_and_responses', 0,1)
  4316. go
  4317. create procedure sp_MSadd_distributor_alerts_and_responses
  4318.     @from_scripting bit = 0
  4319. as
  4320.  
  4321.     declare @description    nvarchar(500)
  4322.     declare @category_name  sysname
  4323.     declare @agent_name     sysname
  4324.     declare @response_job   nvarchar(100)
  4325.     declare @alert_name     nvarchar(100)
  4326.     declare @alert_id       int
  4327.     declare @retcode        int
  4328.  
  4329.     --
  4330.     -- Add replication alert response jobs
  4331.     --
  4332.     select @category_name = name FROM msdb.dbo.syscategories where category_id = 18
  4333.  
  4334.     -- Add Validation failure response job
  4335.     -- (20570,10,0,'Reinitialize Subscriptions On Validation Failure.', 1033)   -- Title of a alert response job
  4336.     -- (20571,10,0,'Reiniitializes all subscriptions that have failed due to data validation failures.', 1033)  -- Description of a alert response job
  4337.     set @response_job = formatmessage(20570)
  4338.     set @description = formatmessage(20571)
  4339.     
  4340.     if @from_scripting = 0
  4341.     begin
  4342.         -- Delete the job if it already exists
  4343.         if (exists (select * from msdb..sysjobs_view where name = @response_job collate database_default))
  4344.         begin
  4345.             exec @retcode = msdb.dbo.sp_delete_job @job_name = @response_job
  4346.             if @@ERROR <> 0 or @retcode <> 0
  4347.                 return (1)
  4348.         end
  4349.  
  4350.         execute @retcode = msdb.dbo.sp_MSadd_repl_job 
  4351.             @response_job,
  4352.             @subsystem = 'TSQL',
  4353.             @server = @@SERVERNAME,
  4354.             @databasename = 'master',
  4355.             @description = @description,
  4356.             @command = 'exec dbo.sp_MSreinit_failed_subscriptions @failure_level = 1',
  4357.             @enabled = 1,
  4358.             @loghistcompletionlevel = 0,
  4359.             @retryattempts = 0,
  4360.             @category_name = @category_name
  4361.         if @@ERROR <> 0 or @retcode <> 0
  4362.             return (1)
  4363.     end
  4364.  
  4365.     --
  4366.     -- Add replication alerts
  4367.     --
  4368.  
  4369.     -- Get Replication category name (assumes category_id = 20)
  4370.     select @category_name = name FROM msdb.dbo.syscategories where category_id = 20
  4371.  
  4372.     -- Add Failure Alert
  4373.     set @alert_name = formatmessage(20536)  
  4374.     set @alert_id = 14151 -- corresponding to formatmessage(20536),  Failure alert
  4375.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  4376.     begin
  4377.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 14151
  4378.         if @@error <> 0 or @retcode <> 0
  4379.             return 1
  4380.     end
  4381.  
  4382.     -- Add Retry Alert
  4383.     set @alert_name=formatmessage(20537)
  4384.     set @alert_id = 14152 -- corresponding to formatmessage(20537),  Retry alert
  4385.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  4386.     begin
  4387.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 14152
  4388.         if @@error <> 0 or @retcode <> 0
  4389.             return 1
  4390.     end
  4391.  
  4392.     -- Add Success Alert
  4393.     set @alert_name=formatmessage(20540)
  4394.     set @alert_id = 14150 -- corresponding to formatmessage(20540), 
  4395.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  4396.     begin
  4397.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 14150
  4398.         if @@error <> 0 or @retcode <> 0
  4399.             return 1
  4400.     end
  4401.  
  4402.     -- Add Shutdown request Alert
  4403.     set @alert_name=formatmessage(20578)
  4404.     set @alert_id = 20578 --  Custom agent shutdown message
  4405.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  4406.     begin
  4407.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 20578
  4408.         if @@error <> 0 or @retcode <> 0
  4409.             return 1
  4410.     end
  4411.  
  4412.     -- Add Validation Failure Alert
  4413.     set @alert_name=formatmessage(20565)
  4414.     set @alert_id = 20574 -- corresponding to formatmessage(20574), 
  4415.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  4416.     begin
  4417.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 20574
  4418.         if @@error <> 0 or @retcode <> 0
  4419.             return 1
  4420.     end
  4421.  
  4422.     -- Add Validation Success Alert
  4423.     set @alert_name=formatmessage(20566)
  4424.     set @alert_id = 20575 -- corresponding to formatmessage(20575), 
  4425.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  4426.     begin
  4427.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 20575
  4428.         if @@error <> 0 or @retcode <> 0
  4429.             return 1
  4430.     end
  4431.  
  4432.     -- Add Reinitialized after Validation Failure
  4433.     set @alert_name=formatmessage(20573)
  4434.     set @alert_id = 20572 -- corresponding to formatmessage(20566),  
  4435.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  4436.     begin
  4437.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 20572
  4438.         if @@error <> 0 or @retcode <> 0
  4439.             return 1
  4440.     end
  4441.  
  4442. GO
  4443.  
  4444.  
  4445. raiserror('Creating procedure sp_MSdrop_distributor_alerts_and_responses', 0,1)
  4446. go
  4447. create proc sp_MSdrop_distributor_alerts_and_responses
  4448. as
  4449.  
  4450.     declare @name           nvarchar(100)
  4451.     declare @alert_id       int
  4452.     declare @retcode        int
  4453.  
  4454.     --
  4455.     -- Delete alerts and response jobs
  4456.     --
  4457.  
  4458.     -- Drop Replication Checkup Agent
  4459.     select @name = formatmessage(20533)
  4460.     IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @name collate database_default and
  4461.         UPPER(originating_server) = UPPER(CONVERT(sysname, SERVERPROPERTY('ServerName'))))
  4462.     BEGIN
  4463.         EXEC @retcode = msdb.dbo.sp_delete_job  @job_name = @name
  4464.         IF @@ERROR <> 0 or @retcode <> 0
  4465.         BEGIN
  4466.             return (1)            
  4467.         END
  4468.     END
  4469.  
  4470.     -- Drop Reinit subscription response job
  4471.     set @name = formatmessage(20570)
  4472.     IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @name collate database_default and
  4473.         UPPER(originating_server) = UPPER(CONVERT(sysname, SERVERPROPERTY('ServerName'))))
  4474.     BEGIN
  4475.         EXEC @retcode = msdb.dbo.sp_delete_job  @job_name = @name
  4476.         IF @@ERROR <> 0 or @retcode <> 0
  4477.         BEGIN
  4478.             return (1)            
  4479.         END
  4480.     END
  4481.  
  4482.     -- Drop the alerts
  4483.     set @alert_id = 14150 -- success alert
  4484.     set @name=formatmessage(20540)
  4485.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4486.     begin
  4487.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4488.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4489.         if @@error <> 0 or @retcode <> 0
  4490.             return (1)            
  4491.     end
  4492.     
  4493.     set @alert_id = 14151 -- failure alert
  4494.     set @name = formatmessage(20536)
  4495.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4496.     begin
  4497.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4498.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4499.         if @@error <> 0 or @retcode <> 0
  4500.             return (1)            
  4501.     end
  4502.     
  4503.     set @alert_id = 14152 -- retry alert
  4504.     set @name = formatmessage(20537)
  4505.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4506.     begin
  4507.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4508.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4509.         if @@error <> 0 or @retcode <> 0
  4510.             return (1)            
  4511.     end
  4512.  
  4513.     set @alert_id = 14153 -- warnning alert
  4514.     set @name = formatmessage(20540)
  4515.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4516.     begin
  4517.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4518.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4519.         if @@error <> 0 or @retcode <> 0
  4520.             return (1)            
  4521.     end
  4522.  
  4523.     -- Remove Validation Failure Alert
  4524.     set @alert_id = 20574 
  4525.     set @name = formatmessage(20565)
  4526.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4527.     begin
  4528.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4529.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4530.         if @@error <> 0 or @retcode <> 0
  4531.             return (1)            
  4532.     end
  4533.  
  4534.     -- Remove Validation Sucess Alert
  4535.     set @alert_id = 20575
  4536.     set @name = formatmessage(20566)
  4537.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4538.     begin
  4539.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4540.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4541.         if @@error <> 0 or @retcode <> 0
  4542.             return (1)            
  4543.     end
  4544.     
  4545.     -- Remove Reinitialized after Validation Failure
  4546.     set @alert_id = 20525 -- checksum alert
  4547.     set @name = formatmessage(20573)
  4548.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4549.     begin
  4550.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4551.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4552.         if @@error <> 0 or @retcode <> 0
  4553.             return (1)            
  4554.     end
  4555.  
  4556.     -- Remove subscription reinitialized after validation failure
  4557.     set @alert_id = 20572 -- corresponding to formatmessage(20566),  
  4558.     set @name=formatmessage(20573)
  4559.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4560.     begin
  4561.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4562.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4563.         if @@error <> 0 or @retcode <> 0
  4564.             return (1)            
  4565.     end
  4566.  
  4567.     -- Remove Shutdown request Alert
  4568.     set @alert_id = 20578 --  Custom agent shutdown message
  4569.     set @name=formatmessage(20578)
  4570.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4571.     begin
  4572.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4573.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4574.         if @@error <> 0 or @retcode <> 0
  4575.             return (1)            
  4576.     end
  4577.  
  4578. GO
  4579. raiserror('Creating procedure sp_adddistributor', 0,1)
  4580. go
  4581.  
  4582. CREATE PROCEDURE sp_adddistributor (
  4583.     @distributor sysname,            /* distributor server name */
  4584.     @heartbeat_interval int = 10,    -- minutes
  4585.     @password sysname = NULL,
  4586.     @from_scripting bit = 0
  4587.  
  4588. ) AS
  4589.  
  4590.     SET NOCOUNT ON
  4591.  
  4592.     /*
  4593.     ** Declarations.
  4594.     */
  4595.     DECLARE @retcode int
  4596.     DECLARE @agentname nvarchar(100)
  4597.     DECLARE @command nvarchar(255)
  4598.     DECLARE @distribution_db sysname
  4599.     DECLARE @distproc nvarchar(255)
  4600.     DECLARE @dist_rpcname sysname
  4601.     DECLARE @server_added bit
  4602.     declare @login sysname
  4603.  
  4604.     select @server_added = 0 
  4605.     select @login = 'distributor_admin'
  4606.  
  4607.     /* 
  4608.     ** Check if replication components are installed on this server
  4609.     */
  4610.     exec @retcode = dbo.sp_MS_replication_installed
  4611.     if (@retcode <> 1)
  4612.     begin
  4613.         return (1)
  4614.     end
  4615.  
  4616.     -- Must be at master db.
  4617.     IF db_name() <> 'master'
  4618.     BEGIN
  4619.         RAISERROR(5001, 16,-1)
  4620.         return (1)
  4621.     END
  4622.     /*
  4623.     ** Parameter Check:  @distributor.
  4624.     ** Check to make sure that the distributor is not NULL and that it
  4625.     ** conforms to the rules for identifiers.
  4626.     */
  4627.     IF @distributor IS NULL
  4628.         BEGIN
  4629.             RAISERROR (14043, 16, -1, '@distributor')
  4630.             RETURN (1)
  4631.         END
  4632.  
  4633.     EXECUTE @retcode = dbo.sp_validname @distributor
  4634.  
  4635.     IF @@ERROR <> 0 OR @retcode <> 0
  4636.        RETURN (1)
  4637.  
  4638.     IF @password = N''
  4639.         select @password = NULL
  4640.     
  4641.     -- Seed default password with random value only for a local distributor.
  4642.     IF (@password is null) AND (UPPER(@@SERVERNAME) = UPPER(@distributor)) 
  4643.         select @password = convert(sysname, newid())
  4644.     /*
  4645.     ** Check to make sure that the distributor doesn't already exist.
  4646.     */
  4647.     SELECT @dist_rpcname = NULL
  4648.     SELECT @dist_rpcname = srvname FROM master..sysservers
  4649.               WHERE  srvstatus & 8 <> 0
  4650.     IF @dist_rpcname IS NOT NULL
  4651.     BEGIN
  4652.         RAISERROR (14099, 16, -1, @dist_rpcname)
  4653.         RETURN(1)
  4654.     END
  4655.  
  4656.     -- drop repl_distributor if it exists.
  4657.     if exists (select * from master..sysservers where lower(srvname) = 
  4658.         'repl_distributor' collate database_default)
  4659.     begin
  4660.         exec @retcode = dbo.sp_dropserver 'repl_distributor', 'droplogins'
  4661.         IF @@error <> 0 OR @retcode <> 0
  4662.         BEGIN
  4663.             RETURN(1)
  4664.         END
  4665.     end
  4666.  
  4667.     -- Add the linked server entry for the distributor
  4668.     -- Note we do this even for local server for consistancy
  4669.     EXECUTE @retcode = dbo.sp_addserver  'repl_distributor'
  4670.     IF @@error <> 0 OR @retcode <> 0
  4671.     BEGIN
  4672.         RETURN (1)
  4673.     END
  4674.  
  4675.     select @server_added = 1
  4676.  
  4677.     -- Mark system link
  4678.     EXECUTE @retcode = dbo.sp_serveroption  'repl_distributor', 'system','true'
  4679.     IF @@error <> 0 OR @retcode <> 0
  4680.     BEGIN
  4681.         RETURN (1)
  4682.     END
  4683.  
  4684.     EXECUTE @retcode = dbo.sp_setnetname  'repl_distributor', @distributor
  4685.     IF @@error <> 0 OR @retcode <> 0
  4686.     BEGIN
  4687.         goto UNDO
  4688.     END
  4689.         
  4690.     exec @retcode = dbo.sp_addlinkedsrvlogin 
  4691.         @rmtsrvname= 'repl_distributor',
  4692.         @useself = 'false',
  4693.         @locallogin = NULL,
  4694.         @rmtuser = @login,
  4695.         @rmtpassword = @password
  4696.     IF @@error <> 0 OR @retcode <> 0
  4697.     BEGIN
  4698.         goto UNDO
  4699.     END
  4700.  
  4701.  
  4702.     /*
  4703.     ** If this is not the local server, remote distributor must be set up first
  4704.     */
  4705.     IF UPPER(@distributor) <> UPPER(@@SERVERNAME)
  4706.     BEGIN
  4707.     /* Check distributor version, 7.0 distributor should error out*/
  4708.     declare @dist_ver int
  4709.     select @dist_ver = 0
  4710.     EXEC @retcode = repl_distributor.master.dbo.sp_executesql N'select @dist_ver = @@microsoftversion', 
  4711.                                     N'@dist_ver int output', @dist_ver output
  4712.     IF (@retcode <> 0 or @@ERROR <> 0)
  4713.     begin
  4714.             GOTO UNDO
  4715.     end
  4716.     if (@dist_ver < 0x07320000 )
  4717.     begin
  4718.             RAISERROR (21320,16,-1)
  4719.             GOTO UNDO
  4720.     end
  4721.         /*
  4722.         ** Test to see if the local server is defined as publisher
  4723.         ** at the remove distributor.
  4724.         ** Note: cannot call sp_helpdistributor locally since the server is not
  4725.         ** marked for distribution.
  4726.         ** We can not move the serveroption call before this RPC because RPC failure
  4727.         ** may cause the SP to terminate. Thus, we can not UNDO the server option.
  4728.         */
  4729.         SELECT @distproc = 'repl_distributor.master.dbo.sp_helpdistributor'
  4730.     
  4731.         DECLARE @loc_directory             nvarchar(255)
  4732.         DECLARE @loc_account             nvarchar(255)
  4733.         DECLARE @loc_mindistretention     int
  4734.         DECLARE @loc_maxdistretention     int
  4735.         DECLARE @loc_historyretention   int  
  4736.         DECLARE @loc_historycleanupagent nvarchar(100)
  4737.         DECLARE @loc_distribcleanupagent nvarchar(100)
  4738.         DECLARE @alert_name nvarchar(100)
  4739.         DECLARE @alert_id int
  4740.  
  4741.         EXECUTE @distproc
  4742.             @distributor OUTPUT,
  4743.             @distribution_db OUTPUT,
  4744.             @loc_directory OUTPUT,
  4745.             @loc_account OUTPUT,
  4746.             @loc_mindistretention OUTPUT,
  4747.             @loc_maxdistretention OUTPUT,
  4748.             @loc_historyretention OUTPUT,
  4749.             @loc_historycleanupagent OUTPUT,
  4750.             @loc_distribcleanupagent OUTPUT,
  4751.             @@SERVERNAME,
  4752.             @local = 'local'
  4753.  
  4754.         IF @@error <> 0 OR @retcode <> 0 OR @distribution_db is NULL
  4755.         BEGIN
  4756.             RAISERROR (21007,16,-1)
  4757.             GOTO UNDO
  4758.         END
  4759.  
  4760.         /* Activate the dist publisher at the remote distributor */
  4761.         SELECT @distproc = 'repl_distributor.master.dbo.sp_changedistpublisher'
  4762.             EXECUTE @retcode = @distproc @@SERVERNAME, 'active','true'
  4763.         IF @@error <> 0 OR @retcode <> 0
  4764.         BEGIN
  4765.             GOTO UNDO
  4766.         END
  4767.     END
  4768.     ELSE
  4769.     /* set the registry */
  4770.     BEGIN
  4771.         EXEC @retcode = dbo.sp_MScreate_distributor_tables 
  4772.         if @@error <> 0 or @retcode <> 0
  4773.             goto UNDO
  4774.  
  4775.         declare @distributor_login sysname
  4776.         select @distributor_login = 'distributor_admin'
  4777.  
  4778.         -- Add publisher rpc login
  4779.         if not exists (select * from master..syslogins where loginname = @distributor_login collate database_default)
  4780.         begin
  4781.             EXEC @retcode = dbo.sp_addlogin @loginame = @distributor_login,
  4782.                 @passwd = @password
  4783.             if @@error <> 0 or @retcode <> 0
  4784.                 goto UNDO
  4785.         end
  4786.         else
  4787.         begin
  4788.             -- Change the password if the distributor is local
  4789.             EXEC @retcode = dbo.sp_password NULL, @password, 'distributor_admin'
  4790.             if @@error <> 0 or @retcode <> 0
  4791.                 goto UNDO
  4792.         end
  4793.  
  4794.         -- Add the login to sysadmin
  4795.         -- Refer to sp_MSpublication_access in distribution db
  4796.         if is_srvrolemember('sysadmin', @distributor_login) <> 1
  4797.         begin
  4798.             exec @retcode = dbo.sp_addsrvrolemember @distributor_login, 'sysadmin'
  4799.             IF @@error <> 0 OR @retcode <> 0
  4800.                 GOTO UNDO
  4801.         end
  4802.  
  4803.         if @from_scripting <> 1
  4804.         begin
  4805.             -- Add Replication Agent Checkup Agent
  4806.             exec @retcode = dbo.sp_MScreate_replication_checkup_agent @heartbeat_interval = @heartbeat_interval
  4807.             if @@error <> 0 or @retcode <> 0
  4808.                 goto UNDO
  4809.         end
  4810.         
  4811.         delete msdb..MSdistributor where property = 'heartbeat_interval'
  4812.         if @@error <> 0
  4813.             goto UNDO
  4814.         insert into msdb..MSdistributor (property, value) values ('heartbeat_interval',
  4815.             convert(nvarchar(10), @heartbeat_interval))
  4816.         if @@error <> 0 
  4817.             goto UNDO
  4818.  
  4819.         -- Add Replication Alerts and Response Jobs
  4820.         exec @retcode = dbo.sp_MSadd_distributor_alerts_and_responses
  4821.             @from_scripting = @from_scripting
  4822.         if @@error <> 0 or @retcode <> 0
  4823.             goto UNDO
  4824.     
  4825.     END
  4826.  
  4827.  
  4828.     /*
  4829.     ** Set the server option to indicate that this is a distributor.
  4830.     ** 
  4831.     */
  4832.     EXECUTE @retcode = dbo.sp_serveroption 'repl_distributor', 'dist', true
  4833.     IF @@error <> 0 OR @retcode <> 0
  4834.     BEGIN
  4835.         GOTO UNDO
  4836.     END
  4837.  
  4838.     -- Set sp_MSrepl_startup to be a startup stored procedure
  4839.     -- Note: This needs to be after the marking the distribution server
  4840.     exec @retcode = dbo.sp_procoption 'sp_MSrepl_startup', 'startup', 'true' 
  4841.     if @@error <> 0 or @retcode <> 0
  4842.         goto UNDO1
  4843.  
  4844.     exec @retcode = dbo.sp_MSrepl_startup
  4845.     if @@error <> 0 or @retcode <> 0
  4846.         goto UNDO1
  4847.  
  4848.     RETURN (0)
  4849.  
  4850. UNDO1:
  4851.     exec dbo.sp_serveroption 'repl_distributor', 'dist', false
  4852.  
  4853. UNDO:
  4854.     IF @server_added = 1
  4855.     begin
  4856.         -- Drop the remote logins, otherwise, sp_dropserver will fail.
  4857.         EXECUTE dbo.sp_dropserver 'repl_distributor', 'droplogins'
  4858.     end
  4859.     
  4860.     RETURN (1)
  4861. GO
  4862.  
  4863.  
  4864. raiserror('Creating procedure sp_changedistributor_property', 0,1)
  4865. go
  4866.  
  4867. CREATE PROCEDURE sp_changedistributor_property (
  4868.     @property sysname     = NULL,       /* The property to change */
  4869.     @value nvarchar(255)  = NULL        /* The new property value */
  4870.     ) AS
  4871.  
  4872.     declare @retcode int
  4873.     declare @new_heartbeat_interval int 
  4874.  
  4875.     -- Check to make sure the local server is a distributor
  4876.     if not exists (SELECT * FROM master..sysservers
  4877.               WHERE  srvstatus & 8 <> 0 and
  4878.               UPPER(datasource) = UPPER(@@servername) collate database_default)
  4879.     begin
  4880.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  4881.         return(1)
  4882.     end
  4883.  
  4884.     -- Return list of properties if @property is NULL
  4885.     if @property is NULL
  4886.     begin
  4887.         create table #tab1 (properties sysname collate database_default not null)
  4888.         insert into #tab1 values ('heartbeat_interval')
  4889.         select * from #tab1
  4890.         return (0)
  4891.     end
  4892.  
  4893.     if @property is NULL
  4894.     begin
  4895.         if exists (select name from msdb..sysobjects where name = 'MSdistributor')
  4896.         begin
  4897.             select property from msdb..MSdistributor
  4898.             return 0
  4899.         end
  4900.         return 1
  4901.     end
  4902.  
  4903.     -- Check for valid property
  4904.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) NOT IN ('heartbeat_interval')
  4905.     BEGIN
  4906.         RAISERROR (14154, 16, -1)
  4907.         RETURN (1)
  4908.     END
  4909.  
  4910.     if LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'heartbeat_interval'
  4911.     begin
  4912.         if exists (select name from msdb..sysobjects where name = 'MSdistributor')  
  4913.         begin
  4914.             update msdb..MSdistributor set value = @value where property = @property
  4915.             if @@error <> 0
  4916.                 return 1
  4917.             select @new_heartbeat_interval = CONVERT(int, @value)
  4918.             exec @retcode = dbo.sp_MScreate_replication_checkup_agent @heartbeat_interval = 
  4919.                 @new_heartbeat_interval
  4920.             if @@error <> 0 or @retcode <> 0
  4921.                 return 1
  4922.         end
  4923.         else 
  4924.             return 1
  4925.     end
  4926.  
  4927.     return (0)
  4928.  
  4929. GO
  4930.  
  4931. raiserror('Creating procedure sp_helpdistributor_properties', 0,1)
  4932. go
  4933.  
  4934. CREATE PROCEDURE sp_helpdistributor_properties 
  4935.      AS
  4936.  
  4937.     if exists (select name from msdb..sysobjects where name = 'MSdistributor')
  4938.     begin
  4939.         -- There is currently only one property, so this will work
  4940.         select 'heartbeat_interval' = convert(int, value) from msdb..MSdistributor where
  4941.             property = 'heartbeat_interval'
  4942.         return (0)
  4943.     end
  4944.     return (1)
  4945. GO
  4946.  
  4947. raiserror('Creating procedure sp_dropdistributor', 0,1)
  4948. go
  4949.  
  4950. CREATE PROCEDURE sp_dropdistributor
  4951. @no_checks bit = 0,
  4952. @ignore_distributor bit = 0
  4953. AS
  4954.  
  4955.     SET NOCOUNT ON
  4956.  
  4957.     /*
  4958.     ** Declarations.
  4959.     */
  4960.     DECLARE @retcode int
  4961.     DECLARE @distributor sysname
  4962.     DECLARE @agentname nvarchar(100)
  4963.     DECLARE @distbit int
  4964.     DECLARE @distribdb sysname
  4965.     DECLARE @foundSubscriber int
  4966.     DECLARE @proc nvarchar(255) 
  4967.     declare @optname sysname
  4968.     declare @name sysname
  4969.     DECLARE @transpublishdb_bit int
  4970.     DECLARE @mergepublishdb_bit int
  4971.     declare @job_name nvarchar(100)
  4972.     declare @alert_name nvarchar(100)
  4973.  
  4974.     declare @dist_rpcname sysname
  4975.  
  4976.     declare @alert_id   int
  4977.    
  4978.     SELECT @transpublishdb_bit = 1
  4979.     SELECT @mergepublishdb_bit = 4
  4980.     SELECT @foundSubscriber = 0
  4981.     SELECT @distbit = 16
  4982.  
  4983.     -- Get distributor name
  4984.     select @distributor = datasource, @dist_rpcname = srvname from master..sysservers 
  4985.         WHERE srvstatus & 8 <> 0
  4986.     if @distributor is null
  4987.     BEGIN
  4988.         RAISERROR (21043, 16, -1)
  4989.         RETURN(1)
  4990.     END
  4991.  
  4992.     if @no_checks = 1
  4993.     begin
  4994.         -- We are in bruteforce cleanup mode, drop everything.
  4995.         DECLARE hCdropdistributor CURSOR LOCAL FAST_FORWARD FOR
  4996.                 SELECT name, N'publish' FROM master..sysdatabases      
  4997.                     WHERE (category & @transpublishdb_bit) <> 0 
  4998.                 UNION
  4999.                 select name, N'merge publish' from master..sysdatabases
  5000.                     WHERE (category & @mergepublishdb_bit) <> 0
  5001.                 FOR READ ONLY
  5002.  
  5003.         OPEN hCdropdistributor
  5004.         FETCH hCdropdistributor INTO @name, @optname
  5005.     
  5006.         WHILE (@@fetch_status <> -1)
  5007.         BEGIN
  5008.                         
  5009.             EXECUTE @retcode  = dbo.sp_replicationdboption 
  5010.               @dbname = @name,
  5011.               @optname = @optname,
  5012.               @value = 'false',
  5013.               @ignore_distributor = @ignore_distributor
  5014.             
  5015.             IF @@ERROR <> 0 OR @retcode <> 0
  5016.             BEGIN
  5017.                 CLOSE hCdropdistributor
  5018.                 DEALLOCATE hCdropdistributor
  5019.                 RETURN (1)
  5020.             END
  5021.             FETCH hCdropdistributor INTO @name, @optname
  5022.         end
  5023.  
  5024.         CLOSE hCdropdistributor
  5025.         DEALLOCATE hCdropdistributor
  5026.     
  5027.         -- Drop subscriber
  5028.         EXECUTE @retcode  = dbo.sp_dropsubscriber
  5029.           @subscriber = 'all',
  5030.           @ignore_distributor = @ignore_distributor
  5031.  
  5032.         IF @@ERROR <> 0 OR @retcode <> 0
  5033.             RETURN (1)
  5034.  
  5035.         IF UPPER(@distributor) = UPPER(@@SERVERNAME) 
  5036.         begin
  5037.             -- Clean up dist publishers
  5038.             DECLARE hCdropdistributor CURSOR LOCAL FAST_FORWARD FOR
  5039.                     SELECT name FROM msdb..MSdistpublishers
  5040.             FOR READ ONLY
  5041.  
  5042.             OPEN hCdropdistributor
  5043.             FETCH hCdropdistributor INTO @name
  5044.  
  5045.             WHILE (@@fetch_status <> -1)
  5046.             BEGIN
  5047.                 exec @retcode = dbo.sp_dropdistpublisher
  5048.                     @publisher = @name,
  5049.                     @no_checks = 1
  5050.             
  5051.                 IF @@ERROR <> 0 OR @retcode <> 0
  5052.                 BEGIN
  5053.                     CLOSE hCdropdistributor
  5054.                     DEALLOCATE hCdropdistributor
  5055.                     RETURN (1)
  5056.                 END
  5057.                 FETCH hCdropdistributor INTO @name
  5058.             end
  5059.  
  5060.             CLOSE hCdropdistributor
  5061.             DEALLOCATE hCdropdistributor
  5062.  
  5063.             -- Clean up distribution dbs
  5064.             DECLARE hCdropdistributor CURSOR LOCAL FAST_FORWARD FOR
  5065.                     SELECT name FROM msdb..MSdistributiondbs
  5066.             FOR READ ONLY
  5067.  
  5068.             OPEN hCdropdistributor
  5069.             FETCH hCdropdistributor INTO @name
  5070.     
  5071.             WHILE (@@fetch_status <> -1)
  5072.             BEGIN
  5073.                 exec @retcode = dbo.sp_dropdistributiondb
  5074.                     @database = @name
  5075.  
  5076.                 IF @@ERROR <> 0 OR @retcode <> 0
  5077.                 BEGIN
  5078.                     CLOSE hCdropdistributor
  5079.                     DEALLOCATE hCdropdistributor
  5080.                     RETURN (1)
  5081.                 END
  5082.                 FETCH hCdropdistributor INTO @name
  5083.             end
  5084.  
  5085.             CLOSE hCdropdistributor
  5086.             DEALLOCATE hCdropdistributor
  5087.         end
  5088.     end
  5089.     
  5090.     -- If everything should be cleaned up when we reach here with @no_checks = 1
  5091.     /*
  5092.     ** If local distributor, check if there are any distributor databases
  5093.     */
  5094.     IF UPPER(@distributor) = UPPER(@@SERVERNAME) 
  5095.     BEGIN
  5096.         IF EXISTS (SELECT * FROM msdb..MSdistributiondbs)
  5097.         BEGIN
  5098.             RAISERROR (14121, 16, -1, @distributor)
  5099.             RETURN(1)
  5100.         END
  5101.     END
  5102.     ELSE
  5103.     begin
  5104.         -- Check to see if there are database published.
  5105.         if exists (SELECT * FROM master..sysdatabases      
  5106.             WHERE (category & @transpublishdb_bit) <> 0 or 
  5107.             (category & @mergepublishdb_bit) <> 0)
  5108.         begin
  5109.             raiserror(21045, 16, -1)
  5110.             return(1)
  5111.         end
  5112.  
  5113.         -- Check to see if there are subscribers defined.
  5114.         if exists (SELECT *
  5115.                      FROM master..sysservers
  5116.                     WHERE srvstatus & 4 <> 0)
  5117.         begin
  5118.             raiserror(21008, 16, -1)
  5119.             return(1)
  5120.         end
  5121.     end
  5122.  
  5123.  
  5124.     /*
  5125.     ** if @ignore_distributor = 1, we are in bruteforce cleanup mode, don't do RPC.
  5126.     */
  5127.     if @ignore_distributor = 0 
  5128.     begin
  5129.         /*
  5130.         ** Get distribution server information
  5131.         */
  5132.         EXEC @retcode = dbo.sp_helpdistributor @publisher = @@SERVERNAME,
  5133.             @distribdb = @distribdb OUTPUT
  5134.         IF @@error <> 0 OR @retcode <> 0
  5135.         BEGIN
  5136.             RAISERROR (14071, 16, -1)
  5137.             RETURN (1)
  5138.         END
  5139.  
  5140.         IF @distribdb is NOT NULL
  5141.         BEGIN
  5142.             /* 
  5143.             ** Deactivate the dist publisher at the  distributor 
  5144.             ** Only do this if @distribdb is NOT NULL, which means the dist publisher
  5145.             ** if defined.
  5146.             */
  5147.             SELECT @proc = RTRIM(@dist_rpcname) + '.master.dbo.sp_changedistpublisher'
  5148.                 EXECUTE @retcode = @proc @@SERVERNAME, 'active','false'
  5149.             IF @@error <> 0 OR @retcode <> 0
  5150.             BEGIN
  5151.                 RETURN (1)
  5152.             END
  5153.         END
  5154.     end
  5155.  
  5156.  
  5157.     /*
  5158.     ** Clear the server option to indicate that this is a distributor.
  5159.     */
  5160.     EXECUTE @retcode = dbo.sp_serveroption @dist_rpcname, 'dist', false
  5161.     IF @@error <> 0 OR @retcode <> 0
  5162.         BEGIN
  5163.             RETURN(1)
  5164.         END
  5165.  
  5166.     -- Prevent dropping local server entry accidentally if user 
  5167.     -- set 'dist' server option on local server.
  5168.     if UPPER(@dist_rpcname) <> UPPER(@@servername)
  5169.     begin
  5170.         exec @retcode = dbo.sp_dropserver @dist_rpcname, 'droplogins'
  5171.         IF @@error <> 0 OR @retcode <> 0
  5172.         BEGIN
  5173.             RETURN(1)
  5174.         END
  5175.     end
  5176.  
  5177.  
  5178.     -- Drop table after unmark distributor to prevent 
  5179.     -- sp_helpdist* failures.
  5180.  
  5181.     /* If local, Drop replication category and alerts */
  5182.     IF UPPER(@distributor) = UPPER(@@SERVERNAME)
  5183.     BEGIN
  5184.  
  5185.     -- Attempts to drop distributor_login, no big deal if it's still in use and we can't drop, don't quit here.
  5186.  
  5187.            declare @distributor_login sysname
  5188.         select @distributor_login = 'distributor_admin'
  5189.  
  5190.         if exists (select * from master..syslogins where loginname = @distributor_login collate database_default)
  5191.         begin
  5192.             EXEC @retcode = dbo.sp_droplogin @loginame = @distributor_login
  5193.     end
  5194.  
  5195.         -- Drop Distributor Alerts and Jobs
  5196.         exec @retcode = dbo.sp_MSdrop_distributor_alerts_and_responses
  5197.         IF @@ERROR <> 0 or @retcode <> 0
  5198.         BEGIN
  5199.             return (1)            
  5200.         END
  5201.  
  5202.         -- Drop the two system tables.
  5203.         if exists (select * from msdb..sysobjects where name = 'MSdistpublishers'
  5204.             and xtype = 'U')
  5205.             drop table msdb..MSdistpublishers
  5206.     
  5207.         if @@error <> 0 
  5208.             return 1 ;
  5209.  
  5210.         if exists (select * from msdb..sysobjects where name = 'MSdistributiondbs'
  5211.             and xtype = 'U')
  5212.             drop table msdb..MSdistributiondbs
  5213.  
  5214.         if @@error <> 0
  5215.             return 1 ;
  5216.  
  5217.         if exists (select * from msdb..sysobjects where name = 'MSdistributor'
  5218.                 and xtype = 'U')
  5219.             drop table msdb..MSdistributor
  5220.  
  5221.         if @@error <> 0
  5222.             return 1 ;
  5223.  
  5224.         if exists (select * from msdb..sysobjects where name = 'sysreplicationalerts'
  5225.                 and xtype = 'U')
  5226.             drop table msdb..sysreplicationalerts
  5227.  
  5228.         if @@error <> 0
  5229.             return 1 ;
  5230.  
  5231.  
  5232.         if exists (select * from msdb..sysobjects where name = 'MSagent_profiles'
  5233.             and xtype = 'U')
  5234.             drop table msdb..MSagent_profiles
  5235.  
  5236.         if @@error <> 0
  5237.             return 1 ;
  5238.  
  5239.  
  5240.        if exists (select * from msdb..sysobjects where name = 'MSagent_parameters'
  5241.                 and xtype = 'U')
  5242.             drop table msdb..MSagent_parameters
  5243.  
  5244.         if @@error <> 0
  5245.             return 1 ;
  5246.  
  5247.     END
  5248.  
  5249.     -- Nnregister sp_MSrepl_startup as a startup stored procedure
  5250.     exec @retcode = dbo.sp_procoption 'sp_MSrepl_startup', 'startup', 'false' 
  5251.     if @@error <> 0 or @retcode <> 0
  5252.         return 1
  5253.  
  5254.     RETURN (0)        
  5255. GO
  5256.  
  5257. dump tran master with NO_LOG
  5258. go
  5259.  
  5260. raiserror('Creating procedure sp_helpdistributiondb', 0,1)
  5261. go
  5262.  
  5263. CREATE PROCEDURE sp_helpdistributiondb (
  5264.     @database sysname = '%'
  5265.     ) AS
  5266.  
  5267.     SET NOCOUNT ON
  5268.  
  5269.     declare @retcode int
  5270.     declare @cmd    nvarchar(500)
  5271.  
  5272.     /*
  5273.     ** Check to make sure this is a distributor
  5274.     */
  5275.     IF NOT EXISTS (select * from master..sysservers
  5276.               where UPPER(datasource) = UPPER(@@SERVERNAME) collate database_default
  5277.                  AND srvstatus & 8 <> 0)
  5278.     begin
  5279.     if @database <> '%' 
  5280.     begin
  5281.             raiserror (14114, 16, -1, @@SERVERNAME)
  5282.             return(1)
  5283.     end
  5284.     else
  5285.        return(0)
  5286.     end
  5287.  
  5288.     IF NOT EXISTS (select * from msdb..sysobjects where name = 'MSdistributiondbs' and type = 'U')
  5289.     begin
  5290.         raiserror (14071, 16, -1)
  5291.         return(1)
  5292.     end    
  5293.  
  5294.     /*
  5295.     ** Check if database is configured as a distributor database
  5296.     */
  5297.     IF @database <> '%' AND NOT EXISTS (select * from msdb..MSdistributiondbs where name = @database collate database_default)
  5298.     begin
  5299.         raiserror (14117, 16, -1, @database)
  5300.         return(1)
  5301.     end    
  5302.  
  5303.     -- Begin from sp_helpdb
  5304.  
  5305.     /*  Create temp table before any DMP to enure dynamic
  5306.     **  Since we examine the status bits in sysdatabase and turn them
  5307.     **  into english, we need a temporary table to build the descriptions.
  5308.     */
  5309.     create table #spdbdesc
  5310.     (
  5311.         dbid    smallint    null,
  5312.         dbdesc  nvarchar(175)   collate database_default null
  5313.     )
  5314.     if @@error<>0
  5315.     begin
  5316.         return(1)
  5317.     end
  5318.  
  5319.     /* 
  5320.     ** Since we need to execute dynamic SQL to get the list of files, create a temp 
  5321.     ** table to keep file descriptions
  5322.     */
  5323.     create table #spfiledesc
  5324.     (
  5325.         dbid    smallint        not null,
  5326.         fileid  smallint        not null,
  5327.         status  int        not null,
  5328.         size    int             not null,
  5329.         name    sysname         collate database_default not null,
  5330.         filename nvarchar(260)   collate database_default not null
  5331.     )   
  5332.     if @@error<>0
  5333.     begin
  5334.         return(1)
  5335.     end
  5336.  
  5337.     /*
  5338.     **  Initialize #spdbdesc from sysdatabases
  5339.     */
  5340.     insert into #spdbdesc (dbid)
  5341.             select dbid 
  5342.               from master.dbo.sysdatabases d 
  5343.         inner join msdb.dbo.MSdistributiondbs dist
  5344.                 on dist.name = d.name collate database_default 
  5345.              where dist.name like @database collate database_default
  5346.                and has_dbaccess(d.name) = 1
  5347.  
  5348.     declare @name sysname
  5349.  
  5350.     /* 
  5351.     ** Insert the list of all files into #spfiledesc
  5352.     */
  5353.     declare c1 CURSOR LOCAL FAST_FORWARD FOR 
  5354.         select db_name (dbid) from #spdbdesc
  5355.     open c1
  5356.     fetch c1 into @name
  5357.     while @@fetch_status >= 0
  5358.     begin
  5359.  
  5360.         /* Insert row for each database */
  5361.         select @cmd =  ('insert into #spfiledesc
  5362.                 select db_id (N'+ QUOTENAME(@name,'''') + '), fileid, status, size, RTRIM(name), RTRIM(filename) from' 
  5363.                 + ' ' + QUOTENAME(@name) + '.dbo.sysfiles')
  5364.         exec (@cmd)
  5365.         fetch c1 into @name
  5366.     end
  5367.     deallocate c1
  5368.  
  5369.     SELECT dist.name, min_distretention, max_distretention, history_retention,
  5370.         'history_cleanup_agent' =  formatmessage (20567, dist.name),
  5371.         'distributor_cleanup_agent' = formatmessage(20568, dist.name),
  5372.         'status'= 0, 
  5373.  
  5374.         -- Refer to sp_helpdb for the following query.
  5375.         'data_folder' = substring(v_data.filename, 1, len(v_data.filename) - charindex('\', reverse(v_data.filename))),
  5376.         'data_file' = right(v_data.filename, charindex('\', reverse(v_data.filename))-1),
  5377.         'data_file_size' = CONVERT(int, (select low from master.dbo.spt_values
  5378.             where type = 'E'
  5379.                 and number = 1)
  5380.             * convert(dec(15), v_data.size)/ 1048576),
  5381.     
  5382.         'log_folder' = substring(v_log.filename, 1, len(v_log.filename) - charindex('\', reverse(v_log.filename))),
  5383.         'log_file' = right(v_log.filename, charindex('\', reverse(v_log.filename))-1),
  5384.         'log_file_size' = CONVERT(int, (select low from master.dbo.spt_values
  5385.             where type = 'E'
  5386.                 and number = 1)
  5387.             * convert(dec(15), v_log.size)/ 1048576)
  5388.         
  5389.         FROM msdb.dbo.MSdistributiondbs dist,
  5390.         master.dbo.sysdatabases d, #spfiledesc v_data,
  5391.         #spfiledesc v_log
  5392.         WHERE dist.name LIKE @database collate database_default
  5393.             
  5394.             and v_data.dbid = d.dbid 
  5395.             and v_data.dbid > 0 
  5396.             and v_data.status & 2 = 2
  5397.             and v_data.status & 0x40 = 0 -- data
  5398.             and v_data.fileid = (select min(v_data2.fileid) from #spfiledesc v_data2 where
  5399.                     v_data2.dbid = d.dbid 
  5400.                 and v_data2.dbid > 0    
  5401.                 and v_data2.status & 2 = 2
  5402.                 and v_data2.status & 0x40 = 0 ) -- data
  5403.  
  5404.             
  5405.             and v_log.dbid = d.dbid 
  5406.             and v_log.dbid > 0  
  5407.             and v_log.status & 2 = 2
  5408.             and v_log.status & 0x40 = 0x40 -- log
  5409.             and v_log.fileid = (select min(v_log2.fileid) from #spfiledesc v_log2 where
  5410.                     v_log2.dbid = d.dbid 
  5411.                 and v_log2.dbid > 0 
  5412.                 and v_log2.status & 2 = 2
  5413.                 and v_log2.status & 0x40 = 0x40 ) -- log
  5414.  
  5415.             and d.name = dist.name collate database_default
  5416. GO
  5417.  
  5418.  
  5419. dump tran master with no_log
  5420. GO
  5421.  
  5422. raiserror('Creating procedure sp_changedistributiondb', 0,1)
  5423. go
  5424.  
  5425. CREATE PROCEDURE sp_changedistributiondb (
  5426.     @database sysname,
  5427.     @property sysname     = NULL,     /* The property to change */
  5428.     @value nvarchar(255)     = NULL      /* The new property value */
  5429.     ) AS
  5430.  
  5431.     SET NOCOUNT ON
  5432.  
  5433.     /*
  5434.     ** Declarations.
  5435.     */
  5436.     DECLARE @distributor sysname
  5437.     DECLARE @min_distretention     int
  5438.     DECLARE @max_distretention     int
  5439.     DECLARE @retcode             int
  5440.     DECLARE @new_min_distretention     int
  5441.     DECLARE @new_max_distretention     int
  5442.     DECLARE @new_history_retention     int
  5443.     DECLARE @agentname nvarchar(100)
  5444.     DECLARE @command nvarchar(255)
  5445.     DECLARE @security_mode int
  5446.     DECLARE @distbit int
  5447.  
  5448.     SELECT @distbit = 16
  5449.  
  5450.     /*
  5451.     ** Parameter Check:  @property.
  5452.     ** If the @property parameter is NULL, print the options.
  5453.     */
  5454.  
  5455.     IF @property IS NULL
  5456.         BEGIN
  5457.             CREATE TABLE #tab1 (properties sysname collate database_default not null)
  5458.             INSERT INTO #tab1 VALUES ('min_distretention')
  5459.             INSERT INTO #tab1 VALUES ('max_distretention')
  5460.             INSERT INTO #tab1 VALUES ('history_retention')
  5461.             SELECT * FROM #tab1
  5462.             RETURN (0)
  5463.         END
  5464.  
  5465.     /*
  5466.     ** Parameter Check:  @property.
  5467.     ** Check to make sure that @property is a valid property in
  5468.     ** sysarticles.
  5469.     */
  5470.     IF @property IS NULL OR LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) NOT IN 
  5471.                                                     ('min_distretention',
  5472.                                                      'max_distretention',
  5473.                                                      'history_retention')
  5474.              
  5475.         BEGIN
  5476.  
  5477.             RAISERROR (14115, 16, -1, 
  5478.             '''min_distretention'', ''max_distretention'' or ''history_retention''')
  5479.             RETURN (1)
  5480.         END
  5481.  
  5482.  
  5483.     /*
  5484.     ** Check to make sure this is a distributor
  5485.     */
  5486.     IF NOT EXISTS (SELECT * FROM master..sysservers
  5487.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME) collate database_default
  5488.                  AND srvstatus & 8 <> 0)
  5489.     BEGIN
  5490.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  5491.         RETURN(1)
  5492.     END
  5493.  
  5494.     /*
  5495.     ** Check if database is configured as a distributor database
  5496.     */
  5497.     IF NOT EXISTS (SELECT * FROM master..sysdatabases
  5498.               WHERE name = @database collate database_default
  5499.                  AND category & @distbit <> 0)
  5500.     BEGIN
  5501.         RAISERROR (14117, 16, -1, @database)
  5502.         RETURN(1)
  5503.     END
  5504.     
  5505.     /*
  5506.     ** Change the property.
  5507.     */
  5508.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'min_distretention'
  5509.         BEGIN
  5510.             IF @value IS NULL
  5511.                 BEGIN
  5512.                     RAISERROR (14043, 16, -1, '@value') 
  5513.                     RETURN (1)
  5514.                 END
  5515.  
  5516.             /*
  5517.             ** Set the MinDistRetention registry key value
  5518.             */
  5519.             SELECT @new_min_distretention = CONVERT(int, @value)
  5520.  
  5521.             /*
  5522.             ** Get MaxDistRetention value
  5523.             */
  5524.             SELECT @max_distretention = max_distretention FROM msdb..MSdistributiondbs
  5525.                 WHERE name = @database collate database_default
  5526.                 
  5527.             /* 
  5528.             ** Check for invalid retention values 
  5529.             */
  5530.             IF @new_min_distretention < 0
  5531.                 BEGIN
  5532.                        RAISERROR(14106, 16, -1)
  5533.                     RETURN (1)
  5534.                 END
  5535.             IF @new_min_distretention > @max_distretention
  5536.                 BEGIN
  5537.                     RAISERROR(14107, 16, -1) 
  5538.                     RETURN (1)
  5539.                 END
  5540.  
  5541.             UPDATE msdb..MSdistributiondbs SET min_distretention = @new_min_distretention
  5542.                 WHERE name = @database collate database_default
  5543.             IF @@error <> 0 
  5544.                 BEGIN
  5545.                     RETURN (1)
  5546.                 END
  5547.  
  5548.             /*
  5549.             ** Update Distribution Cleanup agent 
  5550.             */
  5551.             select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  5552.             j.job_id = s.job_id and
  5553.             j.category_id = 11 and
  5554.             s.database_name = @database collate database_default
  5555.         
  5556.             SELECT @command =  'EXEC dbo.sp_MSdistribution_cleanup @min_distretention = ' + 
  5557.                 CONVERT(nvarchar(12), @new_min_distretention) + ', @max_distretention = ' +
  5558.                 CONVERT(nvarchar(12), @max_distretention)
  5559.             EXEC @retcode = msdb.dbo.sp_update_jobstep @job_name = @agentname, @step_id = 1, 
  5560.                 @command = @command
  5561.             IF @@error <> 0 OR @retcode <> 0
  5562.                RETURN(1)
  5563.  
  5564.         END
  5565.  
  5566.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'max_distretention'
  5567.         BEGIN
  5568.             IF @value IS NULL
  5569.                 BEGIN
  5570.                     RAISERROR (14043, 16, -1, '@value') 
  5571.                     RETURN (1)
  5572.                 END
  5573.  
  5574.             /*
  5575.             ** Set the MaxDistRetention registry key value
  5576.             */
  5577.             SELECT @new_max_distretention = CONVERT(int, @value)
  5578.  
  5579.             /*
  5580.             ** Get MinDistRetention value
  5581.             */
  5582.             SELECT @min_distretention = min_distretention FROM msdb..MSdistributiondbs
  5583.                 WHERE name = @database collate database_default
  5584.  
  5585.             /* 
  5586.             ** Check for invalid retention values 
  5587.             */
  5588.             IF @new_max_distretention < 0
  5589.                 BEGIN
  5590.                        RAISERROR(14106, 16, -1)
  5591.                     RETURN (1)
  5592.                 END
  5593.             IF @new_max_distretention < @min_distretention
  5594.                 BEGIN
  5595.                     RAISERROR(14107, 16, -1) 
  5596.                     RETURN (1)
  5597.                 END
  5598.  
  5599.             UPDATE msdb..MSdistributiondbs SET max_distretention = @new_max_distretention
  5600.                 WHERE name = @database collate database_default
  5601.             IF @@error <> 0 
  5602.                 BEGIN
  5603.                     RETURN (1)
  5604.                 END
  5605.             /*
  5606.             ** Update Distribution Cleanup agent 
  5607.             */
  5608.             select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  5609.                 j.job_id = s.job_id and
  5610.                 j.category_id = 11 and
  5611.                 s.database_name = @database collate database_default
  5612.             SELECT @command =  'EXEC dbo.sp_MSdistribution_cleanup @min_distretention = ' + 
  5613.                 CONVERT(nvarchar(12), @min_distretention) + ', @max_distretention = ' +
  5614.                 CONVERT(nvarchar(12), @new_max_distretention)
  5615.             EXEC @retcode = msdb.dbo.sp_update_jobstep @job_name = @agentname, @step_id = 1, 
  5616.                 @command = @command
  5617.             IF @@error <> 0 OR @retcode <> 0
  5618.                RETURN(1)
  5619.         END
  5620.  
  5621.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'history_retention'
  5622.         BEGIN
  5623.             IF @value IS NULL
  5624.                 BEGIN
  5625.                     RAISERROR (14043, 16, -1, '@value') 
  5626.                     RETURN (1)
  5627.                 END
  5628.  
  5629.             /*
  5630.             ** Set the HistoryRetention registry key value
  5631.             */
  5632.             SELECT @new_history_retention = CONVERT(int, @value)
  5633.  
  5634.             UPDATE msdb..MSdistributiondbs SET history_retention = @new_history_retention
  5635.                 WHERE name = @database collate database_default
  5636.             IF @@error <> 0 
  5637.                 BEGIN
  5638.                     RETURN (1)
  5639.                 END
  5640.  
  5641.             /*
  5642.             ** Update History Cleanup agent 
  5643.             */
  5644.             select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  5645.                 j.job_id = s.job_id and
  5646.                 j.category_id = 12 and
  5647.                 s.database_name = @database collate database_default
  5648.             SELECT @command =  'EXEC dbo.sp_MShistory_cleanup @history_retention = ' + 
  5649.                 CONVERT(nvarchar(12), @new_history_retention)
  5650.             EXEC @retcode = msdb.dbo.sp_update_jobstep @job_name = @agentname, @step_id = 1, 
  5651.                 @command = @command
  5652.             IF @@error <> 0 OR @retcode <> 0
  5653.                RETURN(1)
  5654.  
  5655.         END
  5656.  
  5657.  
  5658.  
  5659.  
  5660.     /*
  5661.     ** Return succeed.
  5662.     */
  5663.     RAISERROR (14105, 10, -1, @property)
  5664.     RETURN (0)
  5665. go
  5666.  
  5667. raiserror('Creating procedure sp_dropdistributiondb', 0,1)
  5668. go
  5669.  
  5670. CREATE PROCEDURE sp_dropdistributiondb (
  5671.     @database sysname        
  5672.     
  5673. ) AS
  5674.  
  5675.     SET NOCOUNT ON
  5676.     /*
  5677.     ** Declarations.
  5678.     */
  5679.     DECLARE @retcode int
  5680.     DECLARE @agentname nvarchar(100)
  5681.     DECLARE @reg_key nvarchar(255)
  5682.     DECLARE @security_mode int
  5683.     DECLARE    @distbit int
  5684.     DECLARE @distpublisher sysname
  5685.     DECLARE @distdb sysname
  5686.     DECLARE @device_name sysname
  5687.     DECLARE @command nvarchar(255)
  5688.  
  5689.     SELECT @distbit = 16
  5690.  
  5691.     /*
  5692.     ** Check to make sure this is a distributor
  5693.     */
  5694.     IF NOT EXISTS (SELECT * FROM master..sysservers
  5695.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME) collate database_default
  5696.                  AND srvstatus & 8 <> 0)
  5697.     BEGIN
  5698.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  5699.         RETURN(1)
  5700.     END
  5701.  
  5702.     /*
  5703.     ** Check if database is configured as a distributor database
  5704.     */
  5705.     IF NOT EXISTS (SELECT * FROM msdb..MSdistributiondbs WHERE name = @database collate database_default)
  5706.     BEGIN
  5707.         RAISERROR (14117, 16, -1, @database)
  5708.         RETURN(1)
  5709.     END
  5710.  
  5711.     /*
  5712.     ** Check if any DistPublishers are using this database
  5713.     */
  5714.     IF EXISTS (SELECT * FROM msdb..MSdistpublishers WHERE
  5715.         distribution_db = @database collate database_default)
  5716.     BEGIN
  5717.         RAISERROR (14120, 16, -1, @database)
  5718.         RETURN (1)
  5719.     END
  5720.     
  5721.     /*
  5722.     ** Check if the DB is being currently used
  5723.     */
  5724.     IF EXISTS (SELECT * FROM master..sysprocesses WHERE
  5725.         dbid = DB_ID(@database))
  5726.     BEGIN
  5727.         RAISERROR (21122, 16, 1, @database)
  5728.         RETURN (1)
  5729.     END
  5730.        
  5731.  
  5732.     /*
  5733.     ** Drop the history cleanup agent.
  5734.     */
  5735.     -- Get agent name
  5736.     select @agentname = null
  5737.     select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  5738.         j.job_id = s.job_id and
  5739.         j.category_id = 12 and
  5740.         s.database_name = @database collate database_default
  5741.  
  5742.     if @agentname is not null
  5743.     BEGIN
  5744.         EXEC @retcode = msdb.dbo.sp_delete_job 
  5745.             @job_name = @agentname
  5746.         IF @@ERROR <> 0 or @retcode <> 0
  5747.         BEGIN
  5748.             RETURN(1)
  5749.         END
  5750.     END
  5751.  
  5752.     /*
  5753.     ** Drop the distribution cleanup agent.
  5754.     */
  5755.     select @agentname = null
  5756.     select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  5757.                 j.job_id = s.job_id and
  5758.                 j.category_id = 11 and
  5759.                 s.database_name = @database collate database_default
  5760.     if @agentname is not null
  5761.     BEGIN
  5762.         EXEC @retcode = msdb.dbo.sp_delete_job 
  5763.             @job_name = @agentname
  5764.         IF @@ERROR <> 0 or @retcode <> 0
  5765.         BEGIN
  5766.             RETURN(1)
  5767.         END
  5768.     END
  5769.  
  5770.     /*
  5771.     ** Update sysdatabase category bit before dropping the database
  5772.     ** Otherwise, the database can not be dropped.
  5773.     **
  5774.     */
  5775.     UPDATE master..sysdatabases SET category = category & ~@distbit WHERE name = @database collate database_default
  5776.     IF @@ERROR <> 0
  5777.         BEGIN
  5778.             RETURN (1)
  5779.         END
  5780.  
  5781.  
  5782.     -- Drop the distributor db if it exists
  5783.     IF EXISTS (SELECT * FROM master..sysdatabases WHERE name = @database collate database_default)
  5784.     BEGIN
  5785.  
  5786.         CREATE TABLE #db_existed
  5787.         (
  5788.         db_existed bit  NOT NULL
  5789.         )
  5790.  
  5791.         /* Get version stamp */
  5792.         SELECT @command = 'INSERT INTO #db_existed SELECT db_existed FROM ' +
  5793.             QUOTENAME(@database) + '..MSrepl_version'
  5794.     
  5795.         EXEC(@command)
  5796.         IF @@ERROR <> 0
  5797.         BEGIN
  5798.             RETURN(1)       
  5799.         END
  5800.  
  5801.         -- Drop the distribution db only if it is created in sp_adddistributiondb.
  5802.         IF NOT EXISTS (SELECT * FROM #db_existed where db_existed = 0x1)
  5803.         BEGIN
  5804.     
  5805.             -- we're dropping the database, so don't bother to clean up
  5806.             -- individual records.  Just make sure we blow away the synchronization
  5807.             -- files & directories hosted by the file system.
  5808.  
  5809.             SELECT @command = QUOTENAME(@database) + '.dbo.sp_MSdrop_snapshot_dirs'
  5810.             SELECT @command = 'EXEC ' + @command
  5811.             EXEC (@command)
  5812.             IF (@@ERROR <> 0)
  5813.             BEGIN
  5814.                 RETURN (1)
  5815.             END
  5816.  
  5817.             /* Save the device info before dropping the database */
  5818.             CREATE TABLE #distdbdevices (name sysname  collate database_default not null)
  5819.  
  5820.             /* Drop distributor devices */
  5821.             /* Query is copied from sp_helpdb */
  5822.             SELECT @command = 
  5823.                 'INSERT INTO #distdbdevices select DISTINCT name from ' + QUOTENAME(@database) + '.dbo.sysfiles'
  5824.             EXEC (@command)
  5825.             IF @@ERROR <>0
  5826.             BEGIN
  5827.                 RETURN(1)      
  5828.             END
  5829.  
  5830.             /*
  5831.             ** Drop the distribution database
  5832.             */
  5833.             SELECT @command = 'drop database ' + QUOTENAME(@database)
  5834.             EXEC (@command)
  5835.             IF @@ERROR <> 0
  5836.                 BEGIN
  5837.                     /* Mark the database as distribution database again 
  5838.                     ** Otherwise, this sp will fail when it is reentered.
  5839.                     ** It is often the case that if there's an open session
  5840.                     ** on the database, it can not be dropped.
  5841.                     */
  5842.                     RETURN(1)
  5843.                 END
  5844.  
  5845.             /* Drop distributor devices */
  5846.             /* Query is copied from sp_helpdb */
  5847.             /* SQL SERVER 7.0 may drop some auto generated device files, drop drop them here
  5848.                again
  5849.             */
  5850.             DECLARE hCdropdistributiondb CURSOR LOCAL FAST_FORWARD FOR
  5851.                 SELECT DISTINCT dd.name FROM #distdbdevices dd, master.dbo.sysdevices ss
  5852.                     WHERE ss.name = dd.name collate database_default
  5853.             FOR READ ONLY
  5854.     
  5855.             OPEN hCdropdistributiondb
  5856.             FETCH hCdropdistributiondb INTO @device_name
  5857.  
  5858.             WHILE (@@fetch_status <> -1)
  5859.             BEGIN
  5860.                 /* 
  5861.                 ** Device may be used by other databases
  5862.                 ** Ignore all errors 
  5863.                 */
  5864.                 EXEC dbo.sp_dropdevice @device_name, DELFILE
  5865.                 FETCH hCdropdistributiondb INTO @device_name
  5866.             END
  5867.  
  5868.             CLOSE hCdropdistributiondb
  5869.             DEALLOCATE hCdropdistributiondb
  5870.  
  5871.         END
  5872.         -- else, database did exist before it was made the distributor, 
  5873.         -- do full cleanup
  5874.         ELSE
  5875.         BEGIN
  5876.  
  5877.             -- run 'fast clean' routine first. this removes repldata files
  5878.             -- and truncates MSrepl_transactions/commands
  5879.  
  5880.             SELECT @command = QUOTENAME(@database) + '.dbo.sp_MSfast_delete_trans'
  5881.             SELECT @command = 'EXEC ' + @command
  5882.             EXEC (@command)
  5883.             IF (@@ERROR <> 0)
  5884.             BEGIN
  5885.                 RETURN (1)
  5886.             END
  5887.  
  5888.             -- Do distribution cleanup the final time
  5889.             -- this handles those fiddly agent tables & such
  5890.             -- ( and would delete commands/xacts if we hadn't already purged 'em )
  5891.  
  5892.             SELECT @command = QUOTENAME(@database) + 
  5893.                 '.dbo.sp_MSdistribution_cleanup @min_distretention = 0, @max_distretention = 0'
  5894.             SELECT @command = 'EXEC ' + @command
  5895.             EXEC (@command)
  5896.             IF (@@ERROR <> 0)
  5897.             BEGIN
  5898.                 RETURN (1)
  5899.             END
  5900.         END
  5901.     END
  5902.     
  5903.  
  5904.     /*
  5905.     ** Delete the DistributionDB entry
  5906.     */
  5907.  
  5908.     DELETE msdb.dbo.MSdistributiondbs where name = @database collate database_default
  5909.     IF @@error <> 0
  5910.     BEGIN
  5911.         RETURN(1)
  5912.     END
  5913.     
  5914.  
  5915.     RETURN (0)
  5916.                 
  5917. GO
  5918.  
  5919. dump tran master with no_log
  5920. GO
  5921. raiserror('Creating procedure sp_adddistributiondb', 0,1)
  5922. go
  5923.  
  5924. CREATE PROCEDURE sp_adddistributiondb (
  5925.     @database sysname,
  5926.     @data_folder nvarchar(255) = NULL,
  5927.     @data_file nvarchar(255) = NULL,            /* physical file name */
  5928.     @data_file_size int = 2,                    /* Default: 2MB */            
  5929.     @log_folder nvarchar(255) = NULL,
  5930.     @log_file nvarchar(255) = NULL,             /* physical file name */
  5931.     @log_file_size int = 0,
  5932.     @min_distretention int = 0,                 /* min distribution retention period in hours */
  5933.     @max_distretention int = 72,                /* max distribution retention period in hours */
  5934.     @history_retention int = 48,                /* history retention period in hours */
  5935.     @security_mode int = 0,                     /* distributor login security 0 standard 1 integrated */
  5936.     @login sysname = 'sa',                      /* standard login name */
  5937.     @password sysname = NULL,                   /* standard login password */
  5938.     @createmode int = 0,  /* 0: use create db for attach (recommended), 
  5939.                             1: create db or use existing but no attach (this is the old way), 
  5940.                             2: create for instdist and detach only */
  5941.     @from_scripting bit = 0
  5942.     ) AS
  5943.  
  5944.     SET NOCOUNT ON
  5945.  
  5946.     /*
  5947.     ** Declarations.
  5948.     */
  5949.     DECLARE @data_path nvarchar(512)
  5950.     DECLARE @log_path nvarchar(512)
  5951.     
  5952.     DECLARE @data_path_quoted_for_copy nvarchar(512)
  5953.     DECLARE @log_path_quoted_for_copy nvarchar(512)
  5954.  
  5955.     DECLARE @logical_data_file nvarchar(255)
  5956.     DECLARE @logical_log_file nvarchar(255)
  5957.     DECLARE @canneddbdata_file nvarchar(255)
  5958.     DECLARE @canneddblog_file nvarchar(255)
  5959.     DECLARE @filecopy_cmd nvarchar(255)
  5960.     DECLARE @file_exists bit
  5961.     DECLARE @data_file_preexists int
  5962.     DECLARE @log_file_preexists int
  5963.     DECLARE @osql_path nvarchar(260)
  5964.     DECLARE @osql_cmd nvarchar(1000)
  5965.     DECLARE @osql_for_nt int
  5966.     DECLARE @devnum int
  5967.     --DECLARE @num_pages int
  5968.     DECLARE @retcode int
  5969.     DECLARE @reg_key nvarchar(255)
  5970.     DECLARE @agentname nvarchar(100)
  5971.     DECLARE @command nvarchar (2048)
  5972.     DECLARE @distbit int
  5973.     DECLARE @install_path nvarchar(255)
  5974.     DECLARE @mssql_data_path nvarchar(255)
  5975.     DECLARE @on_clause nvarchar(512)
  5976.     DECLARE @logon_clause nvarchar(512)
  5977.     DECLARE @distproc nvarchar(255)
  5978.     DECLARE @major_version int
  5979.     DECLARE @db_exists bit
  5980.     DECLARE @trunc_log_bit int
  5981.     DECLARE @description nvarchar(100)
  5982.     DECLARE @category_name sysname
  5983.     DECLARE @createmode_attach int
  5984.     DECLARE @createmode_noattach int
  5985.     DECLARE @createmode_fordetach int
  5986.  
  5987.     --DECLARE @filegrowth nvarchar(10)
  5988.     DECLARE @data_file_size_str nvarchar(10)
  5989.     DECLARE @log_file_size_str nvarchar(10)
  5990.     DECLARE @platform_nt binary
  5991.  
  5992.     --DECLARE @max_datafile_size int
  5993.     --DECLARE @max_logfile_size int
  5994.     
  5995.     IF @password = N''
  5996.         select @password = NULL
  5997.  
  5998.     select @platform_nt = 0x1
  5999.     --select @filegrowth = N'512KB'
  6000.  
  6001.     -- on error, delete the data and log files only if they didn't pre-exist.
  6002.     -- by default, assume they pre-exist.
  6003.     select @data_file_preexists = 1
  6004.     select @log_file_preexists = 1
  6005.     select @file_exists = 0
  6006.  
  6007.     if (@data_file_size IS NULL) or (@data_file_size = 0)
  6008.         select @data_file_size_str = N'512KB'
  6009.     else
  6010.         select @data_file_size_str = convert(nvarchar(10), @data_file_size)
  6011.  
  6012.     if (@log_file_size IS NULL) or (@log_file_size = 0)
  6013.         select @log_file_size_str = N'512KB'
  6014.     else
  6015.         select @log_file_size_str = convert(nvarchar(10), @log_file_size)
  6016.     
  6017.     --if (@data_file_size > 16)
  6018.     --  select @max_datafile_size = @data_file_size
  6019.     --else
  6020.     --  select @max_datafile_size = 16
  6021.     
  6022.     --if (@log_file_size > 16)
  6023.     --  select @max_logfile_size = @log_file_size
  6024.     --else
  6025.     --  select @max_logfile_size = 16
  6026.  
  6027.     select @createmode_attach = 0, @createmode_noattach = 1, @createmode_fordetach = 2
  6028.     SELECT @trunc_log_bit = 8
  6029.     SELECT @distbit = 16
  6030.  
  6031.     if (@createmode <> @createmode_fordetach)
  6032.     begin
  6033.     
  6034.         /* 
  6035.         ** Check if replication components are installed on this server
  6036.         */
  6037.         exec @retcode = dbo.sp_MS_replication_installed
  6038.         if (@retcode <> 1)
  6039.         begin
  6040.             return (1)
  6041.         end
  6042.     
  6043.         /* 
  6044.         ** Check for invalid security modes
  6045.         */
  6046.         IF @security_mode < 0 OR @security_mode > 1
  6047.         BEGIN
  6048.             RAISERROR(14109, 16, -1)
  6049.             RETURN (1)
  6050.         END
  6051.  
  6052.         IF ( ( @platform_nt != platform() & @platform_nt ) and @security_mode = 1)
  6053.         BEGIN
  6054.             RAISERROR(21038, 16, -1)
  6055.             RETURN (1)
  6056.         END
  6057.     
  6058.         /* 
  6059.         ** Check for invalid retention values 
  6060.         */
  6061.         IF @min_distretention < 0 OR @max_distretention < 0 
  6062.         BEGIN
  6063.             RAISERROR(14106, 16, -1)
  6064.             RETURN (1)
  6065.         END
  6066.         IF @min_distretention > @max_distretention
  6067.         BEGIN
  6068.             RAISERROR(14107, 16, -1) 
  6069.             RETURN (1)
  6070.         END
  6071.  
  6072.         /*
  6073.         ** Check to make sure this is a distributor
  6074.         */
  6075.         IF NOT EXISTS (SELECT * FROM master..sysservers
  6076.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME) collate database_default
  6077.                  AND srvstatus & 8 <> 0)
  6078.         BEGIN
  6079.             RAISERROR (14114, 16, -1, @@SERVERNAME)
  6080.             RETURN(1)
  6081.         END
  6082.     
  6083.         /*
  6084.         ** Check if database is already configured as a distributor database
  6085.         */
  6086.         IF EXISTS (SELECT * FROM msdb..MSdistributiondbs WHERE name = @database collate database_default)
  6087.         BEGIN
  6088.             RAISERROR (14119, 16, -1, @database)
  6089.             RETURN(1)
  6090.         END    
  6091.     end
  6092.  
  6093.     /* 
  6094.     ** Get path to osql client (TOOLS) directory
  6095.     */
  6096.     EXECUTE @retcode = master.dbo.sp_MSgettools_path @osql_path OUTPUT
  6097.     IF ( @retcode <> 0 ) or ( @@ERROR <> 0 ) or ( @osql_path is NULL ) or ( @osql_path = '' )
  6098.     BEGIN
  6099.         GOTO UNDO       
  6100.     END
  6101.  
  6102.     /* 
  6103.     ** Get path to version specific INSTALL directory
  6104.     */
  6105.     exec @retcode = master.dbo.sp_MSget_setup_paths
  6106.         @sql_path = @install_path output,
  6107.         @data_path = @mssql_data_path output
  6108.     IF @retcode <> 0 or @install_path is NULL or @install_path='' or @mssql_data_path = ''
  6109.     BEGIN
  6110.         GOTO UNDO       
  6111.     END
  6112.  
  6113.     IF @data_folder IS NULL or @data_folder = ''
  6114.         select @data_folder = @mssql_data_path + '\DATA'
  6115.  
  6116.     IF @log_folder IS NULL or @log_folder = ''
  6117.         select @log_folder = @mssql_data_path + '\DATA'
  6118.  
  6119.     IF @data_file IS NULL
  6120.         SELECT @data_file = @database + '.MDF'
  6121.  
  6122.     IF @log_file IS NULL
  6123.         SELECT @log_file = @database + '.LDF'
  6124.  
  6125.     if substring(@data_folder, len(@data_folder), 1) = '\'
  6126.     select @data_folder = substring (@data_folder, 1, len(@data_folder) -1)
  6127.     if substring(@log_folder, len(@log_folder), 1) = '\'
  6128.     select @log_folder = substring (@log_folder, 1, len(@log_folder) -1)
  6129.  
  6130.     SELECT @data_path = @data_folder + '\' + @data_file
  6131.     SELECT @log_path = @log_folder + '\' + @log_file
  6132.  
  6133.     SELECT @data_path_quoted_for_copy = '"' + fn_escapecmdshellsymbolsremovequotes(@data_folder) collate database_default + '\' + fn_escapecmdshellsymbolsremovequotes(@data_file) collate database_default + '"'
  6134.     SELECT @log_path_quoted_for_copy = '"' + fn_escapecmdshellsymbolsremovequotes(@log_folder) collate database_default + '\' + fn_escapecmdshellsymbolsremovequotes(@log_file) collate database_default + '"'
  6135.  
  6136.     select @logical_data_file = @database
  6137.  
  6138.     /* 
  6139.     ** Truncate the logical log file name back to 128 characters
  6140.     ** long so the 'CREATE DATABASE' statement won't complain.
  6141.     */
  6142.     /* LEN(@logical_log_file) = LEN(@database) + LEN('_log') and
  6143.        LEN(@logical_log_file) <= 128 implies 
  6144.        LEN(@database) <=124 */
  6145.     IF (LEN(@database) > 124)
  6146.         SELECT @logical_log_file = SUBSTRING(@database, 1, 124) + '_log'  
  6147.     ELSE 
  6148.         SELECT @logical_log_file = @database + '_log'
  6149.  
  6150.     if (@createmode = @createmode_attach)
  6151.     begin
  6152.         select @canneddbdata_file = @mssql_data_path + '\DATA\DISTMDL.MDF'
  6153.         select @canneddblog_file = @mssql_data_path + '\DATA\DISTMDL.LDF'
  6154.  
  6155.         exec dbo.sp_MSget_file_existence @canneddbdata_file, @file_exists OUTPUT
  6156.         if (@file_exists = 0)
  6157.         begin
  6158.             /* Fallback to mode where instdist.sql needs to be run */
  6159.             select @createmode = @createmode_noattach
  6160.         end
  6161.  
  6162.         exec dbo.sp_MSget_file_existence @canneddblog_file, @file_exists OUTPUT
  6163.         if (@file_exists = 0)
  6164.         begin
  6165.             /* Fallback to mode where instdist.sql needs to be run */
  6166.             select @createmode = @createmode_noattach
  6167.         end
  6168.     end
  6169.  
  6170.     /*
  6171.     ** Create the distributor database if it does not exist
  6172.     */
  6173.     IF NOT EXISTS (SELECT * from master..sysdatabases WHERE name = @database collate database_default) AND (@createmode <> @createmode_attach)
  6174.     BEGIN
  6175.  
  6176.         -- Note: Use system's default file growth.
  6177.         IF @logical_data_file IS NOT NULL AND NOT EXISTS (SELECT * FROM master..sysdevices WHERE name = @logical_data_file collate database_default)
  6178.         BEGIN
  6179.             SELECT @on_clause = ' ON (NAME =''' + @logical_data_file + ''',FILENAME=''' + REPLACE( @data_path, '''', '''''' ) + 
  6180.                 ''', SIZE=' + @data_file_size_str + ', MAXSIZE = UNLIMITED)'
  6181.         END
  6182.  
  6183.         IF @logical_log_file IS NOT NULL AND NOT EXISTS (SELECT * FROM master..sysdevices WHERE name = @logical_log_file collate database_default)
  6184.         BEGIN
  6185.             SELECT @logon_clause = ' LOG ON (NAME =''' + @logical_log_file + ''',FILENAME=''' + REPLACE( @log_path, '''', '''''' ) + 
  6186.                 ''', SIZE=' + @log_file_size_str + ', MAXSIZE= UNLIMITED)'          
  6187.         END
  6188.  
  6189.         /*
  6190.         ** Create distributor database
  6191.         */
  6192.         SELECT @command = 'USE master  CREATE DATABASE ' +  QUOTENAME(@database) + 
  6193.             + isnull(@on_clause, ' ') + isnull(@logon_clause, ' ')
  6194.  
  6195.         EXEC (@command)
  6196.         IF @@ERROR <> 0
  6197.             RETURN (1)
  6198.         SELECT @db_exists = 0
  6199.     END
  6200.     ELSE IF NOT EXISTS (SELECT * from master..sysdatabases WHERE name = @database collate database_default) AND (@createmode = @createmode_attach)
  6201.     BEGIN
  6202.     /* DO THE CREATE DATABASE FOR ATTACH STUFF */
  6203.         
  6204.         exec dbo.sp_MSget_file_existence @data_path, @data_file_preexists OUTPUT
  6205.         if (@data_file_preexists = 1)
  6206.         begin
  6207.             raiserror(5170, 16, -1, @data_path)
  6208.             return 1
  6209.         end
  6210.     
  6211.         SELECT @on_clause = ' ON (NAME = ''' + @logical_data_file + ''', FILENAME=''' + REPLACE( @data_path, '''', '''''' ) + ''')'
  6212.         
  6213.         exec dbo.sp_MSget_file_existence @log_path, @log_file_preexists OUTPUT
  6214.         if (@log_file_preexists = 1)
  6215.         begin
  6216.             raiserror(5170, 16, -1, @log_path)
  6217.             return 1
  6218.         end
  6219.  
  6220.         SELECT @logon_clause = ' LOG ON (NAME = ''' + @logical_log_file + ''', FILENAME=''' + REPLACE( @log_path, '''', '''''' ) + ''')'
  6221.  
  6222.         select @filecopy_cmd = 'copy "' + fn_escapecmdshellsymbolsremovequotes(@canneddbdata_file) collate database_default + '" ' + @data_path_quoted_for_copy
  6223.         EXEC @retcode = master..xp_cmdshell @filecopy_cmd, NO_OUTPUT
  6224.         IF @retcode <> 0 OR @@ERROR <> 0
  6225.         BEGIN
  6226.             RAISERROR (14113, 16, -1, @filecopy_cmd, 'instdist.out')
  6227.             return (1)
  6228.         END
  6229.  
  6230.         select @filecopy_cmd = 'copy "' + fn_escapecmdshellsymbolsremovequotes(@canneddblog_file) collate database_default + '" ' + @log_path_quoted_for_copy
  6231.         EXEC @retcode = master..xp_cmdshell @filecopy_cmd, NO_OUTPUT
  6232.         IF @retcode <> 0 OR @@ERROR <> 0
  6233.         BEGIN
  6234.             RAISERROR (14113, 16, -1, @filecopy_cmd, 'instdist.out')
  6235.             return (1)
  6236.         END
  6237.  
  6238.         /*
  6239.         ** Create distributor database
  6240.         */
  6241.         SELECT @command = 'USE master  CREATE DATABASE ' +  QUOTENAME(@database) + 
  6242.             + @on_clause + @logon_clause + ' FOR ATTACH'
  6243.  
  6244.         EXEC (@command)
  6245.         IF @@ERROR <> 0
  6246.         begin
  6247.             RETURN (1)
  6248.         end
  6249.         dbcc dbreindexall(@database, 240) with no_infomsgs
  6250.  
  6251.         SELECT @db_exists = 0
  6252.     END
  6253.     ELSE
  6254.     BEGIN
  6255.         SELECT @db_exists = 1
  6256.     END
  6257.  
  6258.     -- Must make the dist db owned by sa so that the sps in it can select from
  6259.     -- security cache tables in tempdb by owership chain rule.
  6260.     declare @retcode2 int
  6261.     select @retcode2 = 0
  6262.     select @distproc = QUOTENAME(@database) + '.dbo.sp_executesql'
  6263.     SELECT @command = 
  6264.         -- If the db is created by sa or from attach, sa is dbo already.
  6265.         -- sp_changedbowner will fail if the new owner is an user in the db already.
  6266.         -- Addendum: we don't perform the check for existing rows in
  6267.         -- sysusers anymore as the server has implemented special logic 
  6268.         -- that handles such scenarios transparently.
  6269.         -- ' if not exists (select * from sysusers where sid = 0x01) ' +
  6270.         ' exec @retcode2 = dbo.sp_changedbowner ''sa'''
  6271.     EXEC @retcode = @distproc @command, N'@retcode2 int output', @retcode2 output
  6272.     IF @retcode <> 0 or @retcode2 <> 0 or @@ERROR <> 0
  6273.     BEGIN
  6274.         GOTO UNDO
  6275.     END
  6276.  
  6277.     /* Set the database option truncate log on checkpoint & turn off autoclose which is default of win9x*/
  6278.     IF EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE 
  6279.         name = @database collate database_default AND
  6280.         (status & @trunc_log_bit) = 0 )   -- if its not already marked
  6281.     BEGIN
  6282.         EXEC @retcode = dbo.sp_dboption @database, 'trunc. log on chkpt.', 'true'
  6283.         IF @retcode <> 0 OR @@ERROR <> 0
  6284.         BEGIN
  6285.             GOTO UNDO
  6286.         END
  6287.     END
  6288.  
  6289.     EXEC @retcode = dbo.sp_dboption @database, 'autoclose', 'false'
  6290.         IF @retcode <> 0 OR @@ERROR <> 0
  6291.         BEGIN
  6292.             GOTO UNDO
  6293.         END
  6294.  
  6295.     /*
  6296.     **
  6297.     ** Update sysdatabase category bit
  6298.     ** This is to prevent user from dropping the database.
  6299.     **/
  6300.     if (@createmode <> @createmode_fordetach)
  6301.     begin
  6302.         UPDATE master..sysdatabases SET category = category | @distbit WHERE name = @database collate database_default
  6303.         IF @@ERROR <> 0
  6304.         BEGIN
  6305.             GOTO UNDO
  6306.         END
  6307.     end
  6308.  
  6309.     /* 
  6310.     ** Install instdist.sql
  6311.     */
  6312.  
  6313.     if (@createmode <> @createmode_attach) OR (@db_exists = 1)
  6314.     begin
  6315.         if (( platform() & @platform_nt = @platform_nt ))
  6316.             select @osql_for_nt = 1
  6317.         else
  6318.             select @osql_for_nt = 0
  6319.  
  6320.         -- Always use integrated security on WINNT since @login passed-in is for remote 
  6321.         -- subscriber and may not have enough privilege to apply the script
  6322.         IF (@security_mode = 1 or @osql_for_nt = 1) AND NOT (@security_mode = 0 AND @createmode = 2)
  6323.         BEGIN
  6324.             SELECT @osql_cmd = '" "' + fn_escapecmdshellsymbolsremovequotes(@osql_path) collate database_default + '\binn\osql" -E '  
  6325.             if serverproperty('instancename') is not null
  6326.                 SELECT @osql_cmd = @osql_cmd + ' -S"' + fn_escapecmdshellsymbols(@@SERVERNAME) collate database_default + '" '
  6327.         END
  6328.         ELSE
  6329.         BEGIN
  6330.         -- cannot specify -S w/ -E for local execution, SID does not map 
  6331.         if (@osql_for_nt = 1)
  6332.             SELECT @osql_cmd = '" "' + fn_escapecmdshellsymbolsremovequotes(@osql_path) collate database_default + '\binn\osql" -U"' + fn_escapecmdshellsymbols(@login) collate database_default + '" -P"' + 
  6333.                 fn_escapecmdshellsymbols(isnull(@password,'')) collate database_default + '" -S"' + fn_escapecmdshellsymbols(@@SERVERNAME) collate database_default + '" '
  6334.         else
  6335.             SELECT @osql_cmd = '"'   + fn_escapecmdshellsymbolsremovequotes(@osql_path) collate database_default + '\binn\osql" -U"' + fn_escapecmdshellsymbols(@login) collate database_default + '" -P"' + 
  6336.                 fn_escapecmdshellsymbols(isnull(@password,'')) collate database_default + '" -S"' + fn_escapecmdshellsymbols(@@SERVERNAME) collate database_default + '" '
  6337.         END
  6338.     
  6339.         select @osql_cmd = @osql_cmd + '-l60 -t60 '
  6340.  
  6341.         -- We must use -b option to make osql return error code !!
  6342.         SELECT @osql_cmd = @osql_cmd + 
  6343.             ' -d"' + fn_escapecmdshellsymbols(@database) collate database_default + '" -b ' +
  6344.             ' -i' + '"' + fn_escapecmdshellsymbolsremovequotes(@install_path) collate database_default + '\install\instdist.sql"' + 
  6345.             ' -o' + '"' + fn_escapecmdshellsymbolsremovequotes(@install_path) collate database_default + '\install\instdist.out"'
  6346.  
  6347.         if (@osql_for_nt = 1)
  6348.         BEGIN
  6349.             SELECT @osql_cmd = @osql_cmd + ' "'
  6350.         END
  6351.     
  6352.         EXEC @retcode = master..xp_cmdshell @osql_cmd
  6353.         IF @retcode <> 0 OR @@ERROR <> 0
  6354.         BEGIN
  6355.             RAISERROR (14113, 16, -1, @osql_cmd, 'instdist.out')
  6356.             GOTO UNDO       
  6357.         END
  6358.     end
  6359.     
  6360.     if (@createmode <> @createmode_fordetach)
  6361.     begin
  6362.         /* Set db_existed bit in MSrepl_version */
  6363.         IF @db_exists = 1
  6364.         BEGIN
  6365.             SELECT @distproc = 'UPDATE ' +
  6366.                 @database + '..MSrepl_version SET db_existed = 0x1'
  6367.     
  6368.             EXEC(@distproc)
  6369.             IF @@ERROR <> 0
  6370.             BEGIN
  6371.                 GOTO UNDO       
  6372.             END
  6373.         END
  6374.  
  6375.         DELETE msdb.dbo.MSdistributiondbs WHERE name = @database collate database_default
  6376.         IF @@ERROR <> 0
  6377.         BEGIN
  6378.             GOTO UNDO       
  6379.         END
  6380.  
  6381.         INSERT INTO msdb.dbo.MSdistributiondbs VALUES (
  6382.             @database, @min_distretention, @max_distretention, @history_retention
  6383.             )
  6384.         IF @@ERROR <> 0
  6385.         BEGIN
  6386.             GOTO UNDO       
  6387.         END
  6388.  
  6389.         -- This login need db_owner priviledge to call sps in distribution db
  6390.         declare @distributor_login sysname
  6391.         select @distributor_login = 'distributor_admin'
  6392.  
  6393.         select @command = quotename(@database) + '.dbo.sp_MSrepl_dbrole'
  6394.         exec @retcode = @command 'db_owner', @distributor_login, 'add'
  6395.         IF @@error <> 0 OR @retcode <> 0
  6396.             GOTO UNDO
  6397.  
  6398.         if @from_scripting = 0
  6399.         begin
  6400.             /*
  6401.             ** Create the history cleanup agent.
  6402.             */
  6403.             SELECT @agentname = formatmessage (20567, @database)
  6404.             SELECT @command =  'EXEC dbo.sp_MShistory_cleanup @history_retention = ' + 
  6405.                 CONVERT(nvarchar(12), @history_retention)
  6406.  
  6407.             IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @agentname collate database_default and
  6408.                 UPPER(originating_server) = UPPER(CONVERT(sysname, SERVERPROPERTY('ServerName'))))
  6409.             BEGIN
  6410.                 EXEC @retcode = msdb.dbo.sp_delete_job 
  6411.                     @job_name = @agentname
  6412.                 IF @@ERROR <> 0 or @retcode <> 0
  6413.                 BEGIN
  6414.                     GOTO UNDO
  6415.                 END
  6416.             END
  6417.  
  6418.             set @description = formatmessage(20535)
  6419.  
  6420.             -- Get History Cleanup category name (assumes category_id = 12)
  6421.             select @category_name = name FROM msdb.dbo.syscategories where category_id = 12
  6422.  
  6423.             EXECUTE @retcode = dbo.sp_MSadd_repl_job @agentname,
  6424.             @subsystem = 'TSQL',
  6425.             @server = @@SERVERNAME,
  6426.             @databasename = @database,
  6427.             @description = @description,
  6428.             @freqtype = 4,    
  6429.             @freqsubtype = 4,         
  6430.             @freqsubinterval = 10,    /* Number of minutes between runs */ 
  6431.             @command = @command,
  6432.             @enabled = 1,
  6433.             @retryattempts = 0,
  6434.             @loghistcompletionlevel = 0,
  6435.             @category_name = @category_name
  6436.     
  6437.             IF @@ERROR <> 0 or @retcode <> 0
  6438.             BEGIN
  6439.                 GOTO UNDO
  6440.             END
  6441.  
  6442.             /*
  6443.             ** Create the distribution cleanup agent.
  6444.             */
  6445.             SELECT @agentname = formatmessage (20568, @database)
  6446.             SELECT @command =  'EXEC dbo.sp_MSdistribution_cleanup @min_distretention = ' + 
  6447.                 CONVERT(nvarchar(12), @min_distretention) + ', @max_distretention = ' +
  6448.                 CONVERT(nvarchar(12), @max_distretention)
  6449.  
  6450.             IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @agentname collate database_default and
  6451.                 UPPER(originating_server) = UPPER(CONVERT(sysname, SERVERPROPERTY('ServerName'))))
  6452.             BEGIN
  6453.                 EXEC @retcode = msdb.dbo.sp_delete_job 
  6454.                     @job_name = @agentname
  6455.                 IF @@ERROR <> 0 or @retcode <> 0
  6456.                 BEGIN
  6457.                     GOTO UNDO
  6458.                 END
  6459.             END
  6460.  
  6461.             set @description = formatmessage(20541)
  6462.             -- Get Distribution Cleanup category name (assumes category_id = 11)
  6463.             select @category_name = name FROM msdb.dbo.syscategories where category_id = 11
  6464.     
  6465.             EXECUTE @retcode = msdb.dbo.sp_MSadd_repl_job @agentname,
  6466.             @subsystem = 'TSQL',
  6467.             @server = @@SERVERNAME,
  6468.             @databasename = @database,
  6469.             @description = @description,
  6470.             @freqtype = 4,    
  6471.             @freqsubtype = 4,         
  6472.             @freqsubinterval = 10,    /* Number of minutes between runs */ 
  6473.             @command = @command,
  6474.             @retryattempts = 0,
  6475.             @enabled = 0,
  6476.             @loghistcompletionlevel = 0,
  6477.             @category_name = @category_name,
  6478.             -- Start  and end time is 5 min off from the history cleanup, which use the default.
  6479.             @activestarttimeofday = 000500,
  6480.             @activeendtimeofday   = 000459
  6481.  
  6482.             IF @@ERROR <> 0 or @retcode <> 0
  6483.             BEGIN
  6484.                 GOTO UNDO
  6485.             end
  6486.         end
  6487.     end
  6488.     else
  6489.     begin
  6490.         /*detach */
  6491.         dbcc detachdb(@database)
  6492.     end
  6493.     
  6494.     RETURN(0)
  6495.  
  6496. UNDO:
  6497.  
  6498.     IF @db_exists = 0
  6499.         EXECUTE dbo.sp_dropdistributiondb @database
  6500.  
  6501.     /* Need to do it since sp_dropdistributiondb will fail in some cases */
  6502.     UPDATE master..sysdatabases SET category = category & ~@distbit WHERE name = @database collate database_default
  6503.     
  6504.     DELETE msdb.dbo.MSdistributiondbs where name = @database collate database_default
  6505.  
  6506.     /* drop the database and ignore error */
  6507.     IF @db_exists = 0 AND
  6508.         EXISTS (SELECT * from master..sysdatabases WHERE name = @database collate database_default)
  6509.     BEGIN
  6510.         SELECT @command = 'USE master  DROP DATABASE ' +  QUOTENAME(@database) 
  6511.         EXEC (@command)
  6512.     END
  6513.  
  6514.     if (@createmode = @createmode_attach)
  6515.     begin
  6516.         if (@data_file_preexists = 0)
  6517.         begin
  6518.             select @command = 'del ' + @data_path_quoted_for_copy
  6519.             exec master..xp_cmdshell @command
  6520.             --ignore errors
  6521.         end
  6522.         if (@log_file_preexists = 0)
  6523.         begin
  6524.             select @command = 'del ' + @log_path_quoted_for_copy
  6525.             exec master..xp_cmdshell @command
  6526.             --ignore errors
  6527.         end
  6528.     end
  6529.         
  6530.     RETURN(1)        
  6531. GO
  6532.  
  6533. dump tran master with no_log
  6534. GO
  6535.  
  6536. raiserror('Creating procedure sp_dropdistpublisher', 0,1)
  6537. go
  6538.  
  6539. CREATE PROCEDURE sp_dropdistpublisher (
  6540.     @publisher sysname,         /* publisher server name */
  6541.     @no_checks bit = 0
  6542.         ) AS
  6543.  
  6544.     SET NOCOUNT ON
  6545.  
  6546.     DECLARE @distributor sysname
  6547.     DECLARE @distaccount nvarchar(127)
  6548.     DECLARE @proc nvarchar (255)
  6549.     DECLARE @retcode int
  6550.     DECLARE @privilege sysname
  6551.     DECLARE @reg_key nvarchar(255)
  6552.     DECLARE @return_status int
  6553.     DECLARE @foundSubscriber int 
  6554.     DECLARE @distribdb sysname
  6555.     DECLARE @command nvarchar(255)
  6556.     DECLARE @active_value int
  6557.     DECLARE @publish_bit int
  6558.     DECLARE @mergepub_bit int
  6559.  
  6560.  
  6561.     SELECT @return_status = 0
  6562.     SELECT @foundSubscriber = 0
  6563.  
  6564.     SELECT @publish_bit = 1
  6565.     SELECT @mergepub_bit = 4
  6566.  
  6567.     /*
  6568.     ** Parameter Check:  @publisher.
  6569.     ** Check to make sure that the publisher exists, that the name isn't
  6570.     ** NULL, and that the name conforms to the rules for identifiers.
  6571.     */
  6572.  
  6573.     IF @publisher IS NULL
  6574.         BEGIN
  6575.             RAISERROR (14043, 16, -1, '@publisher')
  6576.             RETURN (1)
  6577.         END
  6578.  
  6579.     EXECUTE @retcode = dbo.sp_validname @publisher
  6580.  
  6581.     IF @retcode <> 0
  6582.         RETURN (1)
  6583.  
  6584.     /*
  6585.     ** Get distribution server information for remote RPC
  6586.     ** agent verification.
  6587.     */
  6588.     EXEC @retcode = dbo.sp_helpdistributor @publisher = @publisher,
  6589.         @distributor = @distributor OUTPUT,
  6590.         @distribdb = @distribdb OUTPUT
  6591.     IF @@error <> 0 OR @retcode <> 0
  6592.         BEGIN
  6593.         RAISERROR (14071, 16, -1)
  6594.            RETURN (1)
  6595.     END
  6596.  
  6597.     /*
  6598.     ** Only a local distributor can be modified.
  6599.     */
  6600.     IF UPPER(@distributor) <> UPPER(@@SERVERNAME)
  6601.         BEGIN
  6602.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  6603.         RETURN (1)
  6604.         END
  6605.  
  6606.     IF NOT EXISTS (SELECT *
  6607.     FROM msdb..MSdistpublishers
  6608.             WHERE UPPER(name) = UPPER(@publisher) collate database_default)
  6609.     BEGIN
  6610.         RAISERROR (14080, 11, -1)
  6611.         RETURN (1)
  6612.     END
  6613.  
  6614.  
  6615.     IF UPPER(@publisher) = UPPER(@@SERVERNAME)
  6616.     BEGIN
  6617.         /* 
  6618.         ** If @publisher is local, 
  6619.         ** 1. check to make sure there is no subscriber 
  6620.         ** for the publisher in the distribution database
  6621.         ** 2. check to make sure there are not database enabled for replication
  6622.         */
  6623.         -- Check to see if there are subscribers defined.
  6624.         if exists (SELECT *
  6625.                      FROM master..sysservers
  6626.                     WHERE srvstatus & 4 <> 0)
  6627.         begin
  6628.             raiserror(21047, 16, -1)
  6629.             return(1)
  6630.         end
  6631.         
  6632.         IF EXISTS (SELECT * FROM master..sysdatabases WHERE 
  6633.             (category & @publish_bit) <> 0 OR
  6634.             (category & @mergepub_bit) <> 0)
  6635.         BEGIN 
  6636.             RAISERROR (21033, 16, -1, @@SERVERNAME)
  6637.             RETURN (1)
  6638.         END
  6639.     END
  6640.     ELSE
  6641.     BEGIN
  6642.         if @no_checks = 0
  6643.         begin
  6644.             /* 
  6645.             ** If the publisher is remote, check the status of the distpublisher 
  6646.             ** The status will be inactive if the remote publisher dropped the
  6647.             ** distributor.
  6648.             */
  6649.  
  6650.             IF EXISTS (SELECT * FROM msdb.dbo.MSdistpublishers WHERE
  6651.                 UPPER(name) = UPPER(@publisher) collate database_default and active = 1)
  6652.             BEGIN
  6653.                 RAISERROR (14098, 16, -1, @publisher, @@SERVERNAME)
  6654.                 RETURN (1)
  6655.             END
  6656.         end
  6657.         else
  6658.         begin
  6659.             SELECT @command = @distribdb + '.dbo.sp_MSdistpublisher_cleanup'
  6660.             exec @retcode = @command @publisher
  6661.             if @retcode <> 0 or @@error <> 0
  6662.                 return 1
  6663.         end
  6664.     END
  6665.  
  6666.     DELETE msdb..MSdistpublishers where UPPER(name) = UPPER(@publisher) collate database_default
  6667.     IF @@ERROR <> 0  RETURN (1)
  6668.  
  6669.  
  6670.     declare @fExists int
  6671.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 'sa'
  6672.     if (@fExists = 1)
  6673.     BEGIN
  6674.        EXECUTE @retcode = dbo.sp_dropremotelogin @publisher, 'distributor_admin', sa
  6675.        IF @@ERROR <> 0 OR @retcode <> 0 RETURN (1)
  6676.     END
  6677.  
  6678.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 
  6679.         'distributor_admin'
  6680.     if (@fExists = 1)
  6681.     BEGIN
  6682.        EXECUTE @retcode = dbo.sp_dropremotelogin @publisher, 'distributor_admin',
  6683.             'distributor_admin'
  6684.        IF @@ERROR <> 0 OR @retcode <> 0 RETURN (1)
  6685.     END
  6686.  
  6687. /* SECURITY ********************************
  6688.     IF EXISTS (SELECT * FROM master..sysremotelogins
  6689.        WHERE remoteserverid = (SELECT srvid FROM master..sysservers
  6690.        WHERE UPPER(srvname) = UPPER(@publisher) collate database_default)
  6691.        AND remoteusername = 'sa'
  6692.        AND suid = 1)     --'sa' 
  6693. *********************************/
  6694.  
  6695.  
  6696. /* SECURITY ********************************
  6697.     IF EXISTS (SELECT * FROM master..sysremotelogins
  6698.        WHERE remoteserverid = (SELECT srvid FROM master..sysservers
  6699.        WHERE UPPER(srvname) = UPPER(@publisher) collate databsae_default)
  6700.        AND remoteusername = 'probe'
  6701.        AND suid = 10)     -- 'probe' 
  6702.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'probe', 'probe'
  6703.     if (@fExists = 1)
  6704.     BEGIN
  6705.        EXECUTE @retcode = dbo.sp_remoteoption @publisher, probe, probe, trusted, false
  6706.        IF @@ERROR <> 0 OR @retcode <> 0 RETURN (1)
  6707.     END
  6708. *********************************/
  6709.  
  6710.     return(@return_status)
  6711. GO
  6712.  
  6713. raiserror('Creating procedure sp_adddistpublisher', 0,1)
  6714. go
  6715.  
  6716. CREATE PROCEDURE sp_adddistpublisher (
  6717.     @publisher sysname,      /* publisher server name */
  6718.     @distribution_db sysname,
  6719.     @security_mode int = NULL,
  6720.     @login sysname = 'sa',
  6721.     @password sysname = NULL,
  6722.     @working_directory nvarchar(255),
  6723.     @trusted nvarchar(5) = 'false',
  6724.     @encrypted_password bit = 0,
  6725.     @thirdparty_flag bit = 0
  6726.         ) AS
  6727.  
  6728.     SET NOCOUNT ON
  6729.  
  6730.     /*
  6731.     ** Declarations.
  6732.     */
  6733.  
  6734.     DECLARE @retcode int
  6735.     DECLARE @reg_key nvarchar(255)
  6736.     DECLARE @distbit int
  6737.     DECLARE @active_value int
  6738.     DECLARE @server_added bit
  6739.     DECLARE @proc nvarchar(255)
  6740.     declare @fExists int
  6741.     declare @command nvarchar(255)
  6742.     declare @trusted_id bit
  6743.     declare @platform_nt binary
  6744.     declare @qv_replication varchar(10)
  6745.     declare @qv_replication_unlimited integer
  6746.     declare @qv_value_replication integer
  6747.     declare @enc_password nvarchar(524)
  6748.  
  6749.     select @platform_nt = 0x1
  6750.     select @qv_replication = '2745196162', @qv_replication_unlimited = 0
  6751.  
  6752.     SELECT @distbit = 16
  6753.     SELECT @server_added = 0
  6754.  
  6755.     /* 
  6756.     ** Check if replication components are installed on this server
  6757.     */
  6758.     exec @retcode = dbo.sp_MS_replication_installed
  6759.     if (@retcode <> 1)
  6760.     begin
  6761.         return (1)
  6762.     end
  6763.  
  6764.     IF @working_directory IS NULL or ltrim(rtrim(@working_directory)) = ''
  6765.     BEGIN
  6766.         RAISERROR (14043, 16, -1, '@working_directory')
  6767.         return (1)
  6768.     END
  6769.  
  6770.  
  6771.     /*
  6772.     ** Parameter Check:  @publisher.
  6773.     ** Check to make sure that the publisher is not NULL and that it
  6774.     ** conforms to the rules for identifiers.
  6775.     */
  6776.  
  6777.     IF @publisher IS NULL
  6778.         BEGIN
  6779.             RAISERROR (14043, 16, -1, '@publisher')
  6780.             return (1)
  6781.         END
  6782.  
  6783.     EXECUTE @retcode = dbo.sp_validname @publisher
  6784.  
  6785.     IF @@ERROR <> 0 OR @retcode <> 0
  6786.         return (1)
  6787.  
  6788.     IF @password = N''
  6789.         select @password = NULL
  6790.  
  6791.     /* On REPLICATION_LIMITED server, only local publisher is supported.
  6792.      * Note: The login and password registered for local publisher will be used for 
  6793.      * local agents to login to distributor, thus local publisher has to be installed first.
  6794.      * We choose not to support remote dist publshers on REPLICATION_LIMITED server altogether.
  6795.      * On NT, local agents will always use integrated security to log into 
  6796.      * distributor
  6797.      * Today, REPLICATION_LIMITED means desktop but we check specific sku entry just in case
  6798.     */
  6799.     exec @qv_value_replication = master.dbo.sp_MSinstance_qv @qv_replication    
  6800.  
  6801.     if ( @qv_value_replication != @qv_replication_unlimited ) and ( UPPER(@publisher) <> UPPER(@@servername) )
  6802.     begin
  6803.         -- remote dist publisher is not supported on this server version
  6804.         raiserror(21041,16,-1)
  6805.         return (1)
  6806.     end
  6807.  
  6808.  
  6809.     -- Set default security
  6810.     IF @security_mode IS NULL
  6811.     BEGIN
  6812.         IF (UPPER(@publisher) = UPPER(@@SERVERNAME) and ( platform() & @platform_nt = @platform_nt ) )
  6813.             SELECT @security_mode = 1
  6814.         ELSE
  6815.             SELECT @security_mode = 0
  6816.     END
  6817.  
  6818.     /* 
  6819.     ** Check for invalid security mode
  6820.     */
  6821.     IF @security_mode < 0 OR @security_mode > 1
  6822.         BEGIN
  6823.             RAISERROR(14109, 16, -1)
  6824.             return (1)
  6825.         END
  6826.  
  6827.     IF (UPPER(@publisher) = UPPER(@@SERVERNAME) and ( @platform_nt != platform() & @platform_nt ) and @security_mode = 1)
  6828.     BEGIN
  6829.         RAISERROR(21038, 16, -1)
  6830.         RETURN (1)
  6831.     END
  6832.  
  6833.     -- Encrypt the password
  6834.     select @enc_password = @password
  6835.     IF @encrypted_password = 0
  6836.     BEGIN
  6837.         EXEC @retcode = master.dbo.xp_repl_encrypt @enc_password OUTPUT
  6838.         IF @@error <> 0 OR @retcode <> 0
  6839.             return 1
  6840.     END
  6841.  
  6842.     -- Validate the working directory 
  6843.     -- Remove heading and trailing spaces
  6844.     select @working_directory = RTRIM(LTRIM(@working_directory))
  6845.     
  6846.     -- if the last char is '\', remove it.
  6847.     if substring(@working_directory, len(@working_directory),1) = '\'
  6848.         select @working_directory = substring(@working_directory, 1,
  6849.             len(@working_directory)-1)
  6850.  
  6851.     -- Don't do validation if it is a UNC path due to security problem.
  6852.     -- If the server is started as a service using local system account, we
  6853.     -- don't have access to the UNC path.
  6854.     if substring(@working_directory, 1,2) <> '\\'
  6855.     begin
  6856.         select @command = 'dir "' + fn_escapecmdshellsymbolsremovequotes(@working_directory) collate database_default + '"'
  6857.         exec @retcode = master..xp_cmdshell @command, 'no_output'
  6858.         if @@error <> 0
  6859.             return (1)
  6860.         if @retcode <> 0 
  6861.         begin
  6862.             raiserror (21037, 16, -1, @working_directory)
  6863.             return (1)
  6864.         end
  6865.     end
  6866.  
  6867.     /*
  6868.     ** Parameter Check:  @trusted
  6869.     */
  6870.  
  6871.     -- Security - trusted is an insecure default.
  6872.     -- User must override the secure default by explicitly
  6873.     -- requesting @trusted = 'true'
  6874.  
  6875.     IF LOWER(@trusted collate SQL_Latin1_General_CP1_CS_AS) NOT IN ('true', 'false')
  6876.         BEGIN
  6877.             RAISERROR (14148, 16, -1, '@trusted')
  6878.             RETURN (1)
  6879.         END
  6880.  
  6881.     IF LOWER(@trusted collate SQL_Latin1_General_CP1_CS_AS) = 'true' SELECT @trusted_id = 1
  6882.     ELSE SELECT @trusted_id = 0
  6883.  
  6884.     /*
  6885.     ** Check to make sure this is a distributor
  6886.     */
  6887.     IF NOT EXISTS (SELECT * FROM master..sysservers
  6888.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME) collate database_default
  6889.                  AND srvstatus & 8 <> 0)
  6890.     BEGIN
  6891.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  6892.         return (1)
  6893.     END
  6894.  
  6895.     /*
  6896.     ** Check if database is configured as a distributor database
  6897.     */
  6898.     IF NOT EXISTS (SELECT * FROM msdb..MSdistributiondbs WHERE name = @distribution_db collate database_default)
  6899.     BEGIN
  6900.         RAISERROR (14117, 16, -1, @distribution_db)
  6901.         return (1)
  6902.     END
  6903.  
  6904.     /* Check if publisher is already defined. */
  6905.     IF EXISTS (SELECT *
  6906.          FROM msdb..MSdistpublishers
  6907.         WHERE UPPER(name) = UPPER(@publisher) collate database_default)
  6908.  
  6909.     BEGIN
  6910.         RAISERROR (14074, 16, -1, @publisher)
  6911.         RETURN (1)
  6912.     END
  6913.  
  6914.  
  6915.  
  6916.     IF NOT EXISTS (SELECT *
  6917.              FROM master..sysservers
  6918.             WHERE UPPER(srvname) = UPPER(@publisher) collate database_default)
  6919.  
  6920.     /* Add the server if it does not exist. */
  6921.     BEGIN
  6922.         EXECUTE @retcode = dbo.sp_addserver @publisher
  6923.         IF @@error <> 0 OR @retcode <> 0
  6924.         BEGIN
  6925.             RAISERROR (14075, 16, -1)
  6926.             GOTO UNDO
  6927.         END
  6928.         SELECT @server_added = 1
  6929.     END
  6930.     ELSE
  6931.     BEGIN
  6932.         SELECT @publisher = fn_getpersistedservernamecasevariation(@publisher) collate database_default
  6933.     END
  6934.      
  6935.     /*
  6936.     ** Set the Active  value.
  6937.     ** If the @publisher is local, set it to true.
  6938.     ** Otherwise, set it to false
  6939.     */
  6940.     IF UPPER(@publisher) = UPPER(@@SERVERNAME)
  6941.         SELECT @active_value = 1
  6942.     ELSE
  6943.         SELECT @active_value = 0
  6944.  
  6945.  
  6946.     DELETE msdb.dbo.MSdistpublishers where UPPER(name) = UPPER(@publisher) collate database_default
  6947.     IF @@ERROR <> 0
  6948.     BEGIN
  6949.         GOTO UNDO       
  6950.     END
  6951.  
  6952.     INSERT INTO msdb.dbo.MSdistpublishers VALUES (
  6953.         @publisher, @distribution_db, @working_directory,
  6954.         @security_mode, @login, @enc_password, @active_value, @trusted_id, @thirdparty_flag)
  6955.     IF @@ERROR <> 0
  6956.     BEGIN
  6957.         GOTO UNDO       
  6958.     END
  6959.  
  6960.     -- Add distributor_admin to distribution_admin non trusted mapping
  6961.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, null, 
  6962.         'distributor_admin'
  6963.     if( @fExists = 0 )
  6964.     BEGIN
  6965.  
  6966.         EXECUTE @retcode = dbo.sp_addremotelogin @publisher, 'distributor_admin', 
  6967.             'distributor_admin'
  6968.         IF @@error <> 0 OR @retcode <> 0
  6969.         BEGIN
  6970.             RAISERROR (14075, 16, -1)
  6971.             GOTO UNDO
  6972.         END
  6973.     END
  6974.  
  6975.     -- For 6x publisher, we still need the trusted sa to sa.
  6976.     -- For 6x publisher upgrading to 7.0, distributor_admin to distributor_admin need to be trusted.
  6977.     -- add remotelogin of SA if it doesn't already exist
  6978.     -- If there's a mapping for remote login sa already, we cannot map it to distributor_admin
  6979.     -- this is the case of server upgraded from 6.5.
  6980.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, null, 'sa'
  6981.     if( @fExists = 0 )
  6982.     BEGIN
  6983.         EXECUTE @retcode = dbo.sp_addremotelogin @publisher, 'distributor_admin', 'sa'
  6984.         IF @@error <> 0 OR @retcode <> 0
  6985.         BEGIN
  6986.             RAISERROR (14075, 16, -1)
  6987.             GOTO UNDO
  6988.         END
  6989.     END
  6990.     if @trusted_id = 1
  6991.     begin
  6992.         exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 'sa'
  6993.         if( @fExists = 1 )
  6994.         BEGIN
  6995.             EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 'sa', trusted, true
  6996.             IF @@error <> 0 OR @retcode <> 0
  6997.             BEGIN
  6998.                 RAISERROR (14075, 16, -1)
  6999.                 GOTO UNDO
  7000.             END
  7001.         END
  7002.  
  7003.         EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 'distributor_admin', trusted, true
  7004.         IF @@error <> 0 OR @retcode <> 0
  7005.         BEGIN
  7006.             RAISERROR (14075, 16, -1)
  7007.             GOTO UNDO
  7008.         END
  7009.     END
  7010.  
  7011.  
  7012.     /* Add remotelogin enabling the 'probe' of the publisher to
  7013.     ** RPC for distribution counter information.
  7014.     */
  7015. /* SECURITY ********************************
  7016.     IF NOT EXISTS (SELECT *
  7017.               FROM master..sysremotelogins srl,
  7018.                master..sysservers ss
  7019.              WHERE UPPER(ss.srvname) = UPPER(@publisher) collate database_default
  7020.            AND srl.remoteserverid = ss.srvid
  7021.            AND srl.remoteusername = 'probe'
  7022.        AND srl.suid = 10)    -- 'probe' 
  7023.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'probe', 'probe'
  7024.     if (@fExists = 0)
  7025.     BEGIN
  7026.        EXECUTE @retcode = dbo.sp_addremotelogin @publisher, 'probe', 'probe'
  7027.        IF @@error <> 0 OR @retcode <> 0
  7028.        BEGIN
  7029.         RAISERROR (14075, 16, -1)
  7030.         GOTO UNDO
  7031.        END
  7032.     END
  7033. *********************************/
  7034.     
  7035.     RETURN(0)
  7036.  
  7037. UNDO:
  7038.     -- If the server is marked, drop it
  7039.     IF EXISTS (SELECT *
  7040.          FROM msdb..MSdistpublishers
  7041.         WHERE UPPER(name) = UPPER(@publisher) collate database_default)
  7042.         EXEC dbo.sp_dropdistpublisher @publisher
  7043.  
  7044.     IF @server_added = 1
  7045.         EXEC dbo.sp_dropserver @publisher
  7046.  
  7047.     RETURN(1)
  7048. GO
  7049.  
  7050.  
  7051. raiserror('Creating procedure sp_changedistpublisher', 0,1)
  7052. go
  7053.  
  7054. CREATE PROCEDURE sp_changedistpublisher (
  7055.     @publisher sysname,
  7056.     @property sysname     = NULL,     /* The property to change */
  7057.     @value nvarchar(255)     = NULL      /* The new property value */
  7058.     ) AS
  7059.  
  7060.     SET NOCOUNT ON
  7061.  
  7062.     /*
  7063.     ** Declarations.
  7064.     */
  7065.     DECLARE @retcode int
  7066.     DECLARE @new_database sysname
  7067.     DECLARE @new_security_mode int
  7068.     DECLARE @new_login sysname
  7069.     DECLARE @new_password nvarchar(524)
  7070.     DECLARE @distbit int
  7071.     DECLARE @new_active int
  7072.     DECLARE @new_trusted bit
  7073.     DECLARE @command nvarchar(255)
  7074.     declare @distribdb sysname
  7075.     DECLARE @platform_nt binary
  7076.  
  7077.     SELECT @platform_nt = 0x1
  7078.     SELECT @distbit = 16
  7079.     
  7080.     /*
  7081.     ** Parameter Check:  @property.
  7082.     ** If the @property parameter is NULL, print the options.
  7083.     */
  7084.  
  7085.     IF @property IS NULL
  7086.         BEGIN
  7087.             CREATE TABLE #tab1 (properties sysname collate database_default not null)
  7088.             INSERT INTO #tab1 VALUES ('distribution_db')
  7089.             INSERT INTO #tab1 VALUES ('working_directory')
  7090.             INSERT INTO #tab1 VALUES ('security_mode')
  7091.             INSERT INTO #tab1 VALUES ('login')
  7092.             INSERT INTO #tab1 VALUES ('password')
  7093.             INSERT INTO #tab1 VALUES ('active')
  7094.             INSERT INTO #tab1 VALUES ('trusted')
  7095.             SELECT * FROM #tab1
  7096.             RETURN (0)
  7097.         END
  7098.  
  7099.     /*
  7100.     ** Parameter Check:  @property.
  7101.     ** Check to make sure that @property is a valid property in
  7102.     ** msdb.dbo.MSdistpublishers.
  7103.     */
  7104.     IF @property IS NULL OR LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) NOT IN 
  7105.         ('distribution_db',
  7106.          'working_directory',
  7107.          'security_mode',
  7108.          'login',
  7109.          'password',
  7110.          'active',
  7111.          'trusted')
  7112.         BEGIN
  7113.             RAISERROR (14115, 16, -1, 
  7114.             '''distribution_db'', ''working_directory'', ''security_mode'', ''login'', ''password'', ''active'', or ''trusted''')
  7115.             RETURN (1)
  7116.         END
  7117.  
  7118.     /*
  7119.     ** Check to make sure this is a distributor
  7120.     */
  7121.     IF NOT EXISTS (SELECT * FROM master..sysservers
  7122.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME) collate database_default
  7123.                  AND srvstatus & 8 <> 0)
  7124.     BEGIN
  7125.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  7126.         RETURN(1)
  7127.     END
  7128.  
  7129.     -- Get the distribution db name.
  7130.     select @distribdb = distribution_db from msdb..MSdistpublishers where
  7131.         UPPER(name) = UPPER(@publisher) collate database_default
  7132.  
  7133.     /*
  7134.     ** Change the property.
  7135.     */
  7136.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'distribution_db'
  7137.         BEGIN
  7138.             IF @value IS NULL
  7139.                 BEGIN
  7140.                     RAISERROR (14043, 16, -1, '@value') 
  7141.                     RETURN (1)
  7142.                 END
  7143.  
  7144.             IF @value <> @distribdb and 
  7145.                 EXISTS (SELECT * FROM msdb.dbo.MSdistpublishers WHERE
  7146.                 UPPER(name) = UPPER(@publisher) collate database_default and active = 1)
  7147.             BEGIN
  7148.                 RAISERROR (21046, 16, -1)
  7149.                 RETURN (1)
  7150.             END
  7151.  
  7152.             /*
  7153.             ** Check if database is configured as a distributor database
  7154.             */
  7155.             IF NOT EXISTS (SELECT * FROM master..sysdatabases
  7156.               WHERE name = @value collate database_default
  7157.                  AND category & @distbit <> 0)
  7158.                 BEGIN
  7159.                     RAISERROR (14117, 16, -1, @new_database)
  7160.                     RETURN(1)
  7161.                 END
  7162.  
  7163.             UPDATE msdb..MSdistpublishers SET distribution_db = @value
  7164.                 WHERE UPPER(name) = UPPER(@publisher) collate database_default
  7165.             IF @@error <> 0 
  7166.                 BEGIN
  7167.                     RETURN (1)
  7168.                 END
  7169.         END
  7170.    
  7171.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'working_directory'
  7172.         BEGIN
  7173.             IF @value IS NULL
  7174.                 BEGIN
  7175.                     RAISERROR (14043, 16, -1, '@value') 
  7176.                     RETURN (1)
  7177.                 END
  7178.  
  7179.             
  7180.             -- Validate the working directory 
  7181.             -- Remove heading and trailing spaces
  7182.             select @value = RTRIM(LTRIM(@value))
  7183.  
  7184.             -- if the last char is '\', remove it.
  7185.  
  7186.             if substring(@value, len(@value),1) = '\'
  7187.                 select @value = substring(@value, 1,
  7188.                     len(@value)-1)
  7189.  
  7190.             -- Don't do validation if it is a UNC path due to security problem.
  7191.             -- If the server is started as a service using local system account, we
  7192.             -- don't have access to the UNC path.
  7193.             if substring(@value, 1,2) <> '\\'
  7194.             begin
  7195.                 select @command = 'dir "' + fn_escapecmdshellsymbolsremovequotes(@value) collate database_default + N'"'
  7196.                 exec @retcode = master..xp_cmdshell @command, 'no_output'
  7197.                 if @@error <> 0
  7198.                     return 1
  7199.                 if @retcode <> 0 
  7200.                 begin
  7201.                     raiserror (21037, 16, -1, @value)
  7202.                     return 1
  7203.                 end
  7204.             end
  7205.  
  7206.             UPDATE msdb..MSdistpublishers SET working_directory = @value
  7207.                 WHERE UPPER(name) = UPPER(@publisher) collate database_default
  7208.             IF @@error <> 0 
  7209.                 BEGIN
  7210.                     RETURN (1)
  7211.                 END
  7212.         END
  7213.  
  7214.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'security_mode'
  7215.         BEGIN
  7216.             IF @value IS NULL
  7217.                 BEGIN
  7218.                     RAISERROR (14043, 16, -1, '@value') 
  7219.                     RETURN (1)
  7220.                 END
  7221.  
  7222.             /*
  7223.             ** Set the SecurityMode registry key value
  7224.             */
  7225.             SELECT @new_security_mode = CONVERT(int, @value)
  7226.  
  7227.             /* 
  7228.             ** Check for invalid values 
  7229.             */
  7230.             IF @new_security_mode < 0 OR @new_security_mode > 1
  7231.                 BEGIN
  7232.                     RAISERROR(14109, 16, -1)
  7233.                     RETURN (1)
  7234.                 END
  7235.  
  7236.             IF (UPPER(@publisher) = UPPER(@@SERVERNAME) and ( @platform_nt != platform() & @platform_nt ) and @new_security_mode = 1)
  7237.             BEGIN
  7238.                 RAISERROR(21038, 16, -1)
  7239.                 RETURN (1)
  7240.             END
  7241.  
  7242.             UPDATE msdb..MSdistpublishers SET security_mode = @new_security_mode
  7243.                 WHERE UPPER(name) = UPPER(@publisher) collate database_default
  7244.             IF @@error <> 0 
  7245.                 BEGIN
  7246.                     RETURN (1)
  7247.                 END
  7248.         END
  7249.  
  7250.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'login'
  7251.         BEGIN
  7252.             IF @value IS NULL
  7253.                 BEGIN
  7254.                     RAISERROR (14043, 16, -1, '@value') 
  7255.                     RETURN (1)
  7256.                 END
  7257.  
  7258.             /*
  7259.             ** Set the Login registry key value
  7260.             */
  7261.             SELECT @new_login = CONVERT(sysname, @value)
  7262.  
  7263.             UPDATE msdb..MSdistpublishers SET login = @new_login
  7264.                 WHERE UPPER(name) = UPPER(@publisher) collate database_default
  7265.             IF @@error <> 0 
  7266.                 BEGIN
  7267.                     RETURN (1)
  7268.                 END
  7269.         END
  7270.  
  7271.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'password'
  7272.         BEGIN
  7273.             /*
  7274.             ** Set the Password registry key value
  7275.             */
  7276.             SELECT @new_password = CONVERT(nvarchar(524), @value)
  7277.  
  7278.             -- Encrypt the password
  7279.             EXEC @retcode = master.dbo.xp_repl_encrypt @new_password OUTPUT
  7280.             IF @@error <> 0 OR @retcode <> 0
  7281.                 RETURN (1)
  7282.  
  7283.             UPDATE msdb..MSdistpublishers SET password = @new_password
  7284.                 WHERE UPPER(name) = UPPER(@publisher) collate database_default
  7285.             IF @@error <> 0 
  7286.                 BEGIN
  7287.                     RETURN (1)
  7288.                 END
  7289.          END
  7290.  
  7291.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'active'
  7292.         BEGIN
  7293.             /*
  7294.             ** Check for a valid  value.
  7295.             */
  7296.  
  7297.             IF LOWER(@value collate SQL_Latin1_General_CP1_CS_AS) NOT IN ('true', 'false')
  7298.             BEGIN
  7299.                 RAISERROR (14137, 16, -1)
  7300.                 RETURN (1)
  7301.             END
  7302.  
  7303.             IF LOWER(@value collate SQL_Latin1_General_CP1_CS_AS) = 'true'
  7304.             begin
  7305.                 -- Clean up the database in case of the remote publisher is reinstalling publishing.
  7306.                 SELECT @command = @distribdb + '.dbo.sp_MSdistpublisher_cleanup'
  7307.                 exec @retcode = @command @publisher
  7308.                 if @retcode <> 0 or @@error <> 0
  7309.                     return 1
  7310.                 SELECT @new_active = 1
  7311.             end
  7312.             ELSE
  7313.             BEGIN
  7314.                 SELECT @new_active = 0
  7315.             END
  7316.  
  7317.             /*
  7318.             ** Set the Active registry key value
  7319.             */
  7320.             UPDATE msdb..MSdistpublishers SET active = @new_active
  7321.                 WHERE UPPER(name) = UPPER(@publisher) collate database_default
  7322.             IF @@error <> 0
  7323.                 BEGIN
  7324.                     RETURN (1)
  7325.                 END
  7326.  
  7327.         END
  7328.  
  7329.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'trusted'
  7330.         BEGIN
  7331.             /*
  7332.             ** Check for a valid  value.
  7333.             */
  7334.  
  7335.             IF LOWER(@value collate SQL_Latin1_General_CP1_CS_AS) NOT IN ('true', 'false')
  7336.             BEGIN
  7337.                 RAISERROR (14137, 16, -1)
  7338.                 RETURN (1)
  7339.             END
  7340.  
  7341.             declare @fExists int
  7342.  
  7343.             IF LOWER(@value collate SQL_Latin1_General_CP1_CS_AS) = 'true'
  7344.             begin
  7345.                 SELECT @new_trusted = 1
  7346.                 exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 'sa'
  7347.                 if( @fExists = 1 )
  7348.                 BEGIN
  7349.                     EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 'sa', trusted, true
  7350.                     IF @@error <> 0 OR @retcode <> 0
  7351.                     BEGIN
  7352.                         RAISERROR (14075, 16, -1)
  7353.                         RETURN (1)
  7354.                     END
  7355.                 END
  7356.  
  7357.  
  7358.                 EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 
  7359.                     'distributor_admin', trusted, true
  7360.                 IF @@error <> 0 OR @retcode <> 0
  7361.                 BEGIN
  7362.                     RAISERROR (14075, 16, -1)
  7363.                     RETURN (1)
  7364.                 END
  7365.             end
  7366.             ELSE
  7367.             BEGIN
  7368.                 SELECT @new_trusted = 0
  7369.      
  7370.                 exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 'sa'
  7371.                 if( @fExists = 1 )
  7372.                 BEGIN
  7373.                     EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 'sa', trusted, false
  7374.                     IF @@error <> 0 OR @retcode <> 0
  7375.                     BEGIN
  7376.                         RAISERROR (14075, 16, -1)
  7377.                         RETURN (1)
  7378.                     END
  7379.                 END
  7380.  
  7381.  
  7382.                 EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin',
  7383.                     'distributor_admin', trusted, 'false'
  7384.                 IF @@error <> 0 OR @retcode <> 0
  7385.                 BEGIN
  7386.                     RAISERROR (14075, 16, -1)
  7387.                     RETURN (1)
  7388.                 END
  7389.             END
  7390.  
  7391.             /*
  7392.             ** Set the trusted property
  7393.             */
  7394.             UPDATE msdb..MSdistpublishers SET trusted = @new_trusted
  7395.                 WHERE UPPER(name) = UPPER(@publisher) collate database_default
  7396.             IF @@error <> 0
  7397.                 BEGIN
  7398.                     RETURN (1)
  7399.                 END
  7400.         END
  7401.  
  7402.   
  7403.     /*
  7404.     ** Return succeed.
  7405.     */
  7406.     RAISERROR (21035, 10, -1, @property)
  7407.  
  7408. DONE:    
  7409.     RETURN (0)
  7410. go
  7411.  
  7412. raiserror('Creating procedure sp_helpdistpublisher', 0,1)
  7413. go
  7414.  
  7415. CREATE PROCEDURE sp_helpdistpublisher (
  7416.     @publisher sysname = '%'         /* publisher server name */
  7417.     , @check_user bit = 0
  7418. AS
  7419. BEGIN
  7420.     SET NOCOUNT ON
  7421.     declare @is_sysadmin int
  7422.  
  7423.     select @is_sysadmin = is_srvrolemember('sysadmin')
  7424.     IF @publisher IS null
  7425.     begin
  7426.         raiserror (14043, 16, -1, '@publisher')
  7427.         return (1)
  7428.     end
  7429.  
  7430.     /*
  7431.     ** Check to make sure this is a distributor
  7432.     */
  7433.     IF NOT EXISTS (select * from master..sysservers
  7434.               where UPPER(datasource) = UPPER(@@SERVERNAME) collate database_default
  7435.                  AND srvstatus & 8 <> 0)
  7436.     begin
  7437.         if @publisher <> '%' 
  7438.         begin
  7439.                 raiserror (14114, 16, -1, @@SERVERNAME)
  7440.                 return(1)
  7441.         end
  7442.         else
  7443.             return(0)
  7444.     end
  7445.  
  7446.     IF @publisher <> '%' AND NOT EXISTS (select * from msdb..MSdistpublishers
  7447.             where UPPER(name) = UPPER(@publisher) collate database_default)
  7448.     begin
  7449.         raiserror (14080, 11, -1)
  7450.         return (1)
  7451.     end
  7452.  
  7453.     -- If sysadmin, no need for filtering
  7454.     if @is_sysadmin = 1
  7455.         select @check_user = 0
  7456.  
  7457.     create table #distdbs (name sysname collate database_default)
  7458.  
  7459.     if @check_user = 1
  7460.     begin
  7461.         declare @distbit int, @db_name sysname, @cmd nvarchar(1000)
  7462.         select @distbit = 16
  7463.         declare hCdatabase CURSOR LOCAL FAST_FORWARD FOR
  7464.             select name from master.dbo.sysdatabases 
  7465.                 where
  7466.                 category & @distbit <> 0 and
  7467.                 has_dbaccess(name) = 1
  7468.         for read only
  7469.  
  7470.         open hCdatabase
  7471.         fetch next from hCdatabase into @db_name
  7472.         while (@@fetch_status <> -1)
  7473.         begin
  7474.             
  7475.             -- Check to see if the user has permision to monitor the distribution 
  7476.             -- database.
  7477.             declare @has_pm bit
  7478.             select @cmd = quotename(@db_name) + '.dbo.sp_executesql'
  7479.             exec @cmd
  7480.                 N'if is_member(N''db_owner'') = 1 or is_member(N''replmonitor'') = 1 set @has_pm = 1', 
  7481.                 N'@has_pm bit output',
  7482.                 @has_pm output
  7483.             if @has_pm = 1
  7484.                 insert #distdbs values (@db_name)
  7485.             fetch next from hCdatabase into @db_name
  7486.         end
  7487.         close hCdatabase
  7488.         deallocate hCdatabase
  7489.     end
  7490.  
  7491.     SELECT p.name, distribution_db, security_mode, 
  7492.         -- Not to return login unless sysadmin is called.
  7493.         'login' = case when (@is_sysadmin = 1) then login else cast(NULL as sysname) end,
  7494.         -- Not to return password unless sysadmin is called.
  7495.         'password' = case  when (@is_sysadmin = 1) then password else cast(NULL as sysname) end,
  7496.         active, working_directory, trusted, thirdparty_flag
  7497.     FROM msdb.dbo.MSdistpublishers p
  7498.     where ((@publisher = N'%') or (UPPER(p.name) collate database_default = UPPER(@publisher) collate database_default)) and
  7499.             (@check_user = 0 or exists 
  7500.                 (select * from #distdbs d where p.distribution_db = d.name collate database_default))
  7501.     IF @@ERROR <> 0
  7502.     begin
  7503.         return(1)
  7504.     end
  7505. END
  7506. GO
  7507.  
  7508. dump tran master with no_log
  7509. go
  7510.  
  7511. raiserror('Creating procedure sp_add_agent_profile', 0,1)
  7512. go
  7513.  
  7514. /*
  7515. ** The system profile of the same type of agent will be used as a template for 
  7516. ** the parameters in this new user profile.
  7517. */
  7518. CREATE PROCEDURE sp_add_agent_profile (
  7519.     @profile_id             int = NULL OUTPUT,
  7520.     @profile_name           sysname,
  7521.     @agent_type             int,            -- 1-Snapshot, 2-Logreader, 
  7522.                                             -- 3-Distribution, 4-Merge,
  7523.                                             -- 9-Qreader
  7524.     @profile_type           int = 1,        -- 0-System, 1-Custom 
  7525.     @description            nvarchar(3000) = NULL,
  7526.     @default                bit = 0         -- 0-Not Default, 1-Default
  7527. )
  7528. AS
  7529.     SET NOCOUNT ON
  7530.  
  7531.     declare     @sys_profile    int
  7532.     declare     @default_id     int
  7533.     declare     @sysdefault_id  int
  7534.     declare     @retcode        int
  7535.  
  7536.     SELECT @profile_name = RTRIM(@profile_name)
  7537.  
  7538.     IF @profile_name IS NULL
  7539.     BEGIN
  7540.         RAISERROR (14043, 16, -1, '@profile_name')
  7541.         RETURN (1)
  7542.     END
  7543.  
  7544.     exec @retcode = dbo.sp_MSreplcheck_name @profile_name
  7545.     if @@ERROR <> 0 or @retcode <> 0
  7546.         return(1)
  7547.  
  7548.     /* The profile name is unique across a particular agent type */
  7549.     IF EXISTS ( SELECT * FROM msdb..MSagent_profiles 
  7550.             WHERE profile_name = @profile_name collate database_default
  7551.             AND agent_type = @agent_type )
  7552.     BEGIN
  7553.         RAISERROR(20057, 16, -1, @profile_name)
  7554.         RETURN (1)
  7555.     END
  7556.  
  7557.     IF @agent_type NOT IN (1, 2, 3, 4, 9)
  7558.     BEGIN
  7559.         RAISERROR(20058, 16, -1)
  7560.         return (1)
  7561.     END
  7562.  
  7563.     IF @profile_type NOT IN (0, 1)
  7564.     BEGIN
  7565.         RAISERROR(20059, 16, -1)
  7566.         return (1)
  7567.     END
  7568.  
  7569.     BEGIN TRAN
  7570.  
  7571.     /*
  7572.     ** First find out the default profile of the same agent type
  7573.     */
  7574.     select @default_id = profile_id from msdb..MSagent_profiles WHERE agent_type = @agent_type AND def_profile = 1
  7575.     
  7576.     INSERT INTO msdb..MSagent_profiles
  7577.             VALUES (@profile_name, @agent_type, @profile_type, @description, 0)
  7578.     IF @@ERROR <> 0
  7579.         GOTO UNDO
  7580.  
  7581.     SELECT @profile_id = profile_id 
  7582.         FROM msdb..MSagent_profiles
  7583.         WHERE profile_name = @profile_name collate database_default AND agent_type = @agent_type
  7584.  
  7585.     /*
  7586.     ** If there is system profiles of the same agent type, use the primary system one as template of parameter set
  7587.     */
  7588.     IF EXISTS (select * from msdb..MSagent_profiles where agent_type = @agent_type AND type = 0)
  7589.         select @sysdefault_id = min(profile_id) from msdb..MSagent_profiles where agent_type = @agent_type AND type = 0
  7590.  
  7591.     if @sysdefault_id IS NULL
  7592.         select @sysdefault_id = @default_id
  7593.  
  7594.     if @profile_type=1
  7595.     BEGIN
  7596.             INSERT INTO msdb..MSagent_parameters 
  7597.             select @profile_id, parameter_name, value from msdb..MSagent_parameters 
  7598.                 where profile_id = @sysdefault_id      
  7599.         if @@ERROR<>0 
  7600.             GOTO UNDO
  7601.     END
  7602.  
  7603.     /* Only one profile for an agent type must be default */
  7604.     IF @default = 1
  7605.         BEGIN
  7606.             UPDATE msdb..MSagent_profiles
  7607.                 SET def_profile = 0 WHERE profile_id = @default_id
  7608.             IF @@ERROR <> 0
  7609.                 GOTO UNDO
  7610.             UPDATE msdb..MSagent_profiles
  7611.                 SET def_profile = 1 WHERE profile_id = @profile_id
  7612.             IF @@ERROR <> 0
  7613.                 GOTO UNDO
  7614.         END
  7615.  
  7616.     COMMIT TRAN
  7617.     RETURN 0 
  7618.  
  7619. UNDO:
  7620.  
  7621.     IF @@TRANCOUNT = 1
  7622.         ROLLBACK TRAN
  7623.     ELSE
  7624.         COMMIT TRAN
  7625.  
  7626.     RETURN 1 
  7627. GO
  7628.  
  7629. raiserror('Creating procedure sp_drop_agent_parameter', 0,1)
  7630. go
  7631.  
  7632. -- Drop a/all parameter(s) of a/all profile(s) from the MSagent_parameters table
  7633. create procedure sp_drop_agent_parameter (
  7634.     @profile_id int,
  7635.     @parameter_name     sysname = '%'
  7636. )
  7637. as
  7638.     set nocount on
  7639.  
  7640.     declare @default            bit
  7641.     declare @agent_type         int
  7642.     declare @retstatus          int
  7643.     select @retstatus = 0
  7644.  
  7645.     if @parameter_name is null
  7646.     BEGIN
  7647.         RAISERROR (14043, 16, -1, '@parameter_name')
  7648.         RETURN (1)
  7649.     END
  7650.  
  7651.     select @agent_type = agent_type, @default=def_profile
  7652.     from msdb..MSagent_profiles
  7653.     where profile_id = @profile_id
  7654.  
  7655.     if @default is null
  7656.     BEGIN
  7657.         RAISERROR (20066, 16, -1) -- profile not defined
  7658.         RETURN (1)
  7659.     END
  7660.     
  7661.     select @parameter_name = lower(@parameter_name collate SQL_Latin1_General_CP1_CS_AS)
  7662.  
  7663.     if left(@parameter_name,1) in (N'-', N'/')
  7664.         select @parameter_name = right(@parameter_name,len(@parameter_name)-1)
  7665.  
  7666.     /* Delete the parameters of the profile */
  7667.     delete  msdb..MSagent_parameters 
  7668.     where   right(lower(parameter_name collate SQL_Latin1_General_CP1_CS_AS),len(parameter_name)-1) like 
  7669.             @parameter_name collate SQL_Latin1_General_CP1_CS_AS
  7670.     and     profile_id = @profile_id
  7671.  
  7672.     if @@error <> 0
  7673.     begin
  7674.         select @retstatus = 1
  7675.         goto UNDO
  7676.     end
  7677.  
  7678. UNDO:
  7679.     
  7680.     return @retstatus
  7681. GO
  7682.  
  7683. raiserror('Creating procedure sp_drop_agent_profile', 0,1)
  7684. go
  7685. -- Drop a profile from the MSagent_profiles table, as well as the corresponding
  7686. -- parameters from the MSagent_parameters table
  7687.  
  7688. CREATE PROCEDURE sp_drop_agent_profile (
  7689.     @profile_id int
  7690. )
  7691. AS
  7692.     SET NOCOUNT ON
  7693.  
  7694.     declare @snapshot_type      int
  7695.     declare @logreader_type     int
  7696.     declare @distribution_type  int
  7697.     declare @merge_type         int
  7698.     declare @qreader_type        int
  7699.     
  7700.     declare @tablename          nvarchar(255)
  7701.     declare @proc               nvarchar(255)
  7702.     declare @distribution_db    sysname
  7703.     declare @profile_type       int
  7704.  
  7705.     declare @default            bit
  7706.     declare @usage_count        int
  7707.     declare @agent_type         int
  7708.     declare @retstatus          int
  7709.     DECLARE @retcode            int
  7710.  
  7711.     declare @default_sys_id     int
  7712.  
  7713.     SELECT @agent_type = agent_type, @profile_type = type, @default=def_profile
  7714.     FROM msdb..MSagent_profiles
  7715.     WHERE profile_id = @profile_id
  7716.  
  7717.     IF @default IS NULL
  7718.     BEGIN
  7719.         RAISERROR (20066, 16, -1) -- Profile not defined
  7720.         RETURN (1)
  7721.     END
  7722.  
  7723.     /*
  7724.     ** Before dropping a default profile, one system profile of the same agent type
  7725.     ** has to become the new default profile.
  7726.     */
  7727.     IF @default = 1
  7728.     BEGIN
  7729.         select @default_sys_id=min(profile_id) from msdb..MSagent_profiles
  7730.             where agent_type = @agent_type AND type = 0
  7731.             
  7732.         UPDATE msdb..MSagent_profiles SET def_profile = 1 WHERE profile_id = @default_sys_id
  7733.     END
  7734.  
  7735.     select @snapshot_type = 1
  7736.     select @logreader_type = 2
  7737.     select @distribution_type = 3
  7738.     select @merge_type = 4
  7739.     select @qreader_type = 9
  7740.  
  7741.         /* By default, assume that this profile is not being used */
  7742.     select @usage_count = -1
  7743.  
  7744.     select @tablename = 
  7745.         case @agent_type
  7746.             when @snapshot_type then 'MSsnapshot_agents'
  7747.             when @logreader_type then 'MSlogreader_agents'
  7748.             when @distribution_type then 'MSdistribution_agents'
  7749.             when @merge_type then 'MSmerge_agents'
  7750.             when @qreader_type then 'MSqreader_agents'
  7751.         end
  7752.  
  7753.     declare hCdistdbs CURSOR LOCAL FAST_FORWARD FOR
  7754.         select distinct distribution_db 
  7755.         from msdb..MSdistpublishers
  7756.         for read only
  7757.  
  7758.     open hCdistdbs
  7759.     fetch hCdistdbs into @distribution_db
  7760.  
  7761.     while @@fetch_status <> -1 and @usage_count = -1
  7762.     begin
  7763.         select @proc = @distribution_db + '.dbo.sp_MSprofile_in_use'
  7764.         execute @usage_count = @proc @tablename = @tablename, @profile_id = @profile_id
  7765.     
  7766.         if @@error <> 0
  7767.         begin
  7768.             select @retstatus = 1
  7769.             goto UNDO
  7770.         end
  7771.  
  7772.         fetch hCdistdbs into @distribution_db
  7773.     end
  7774.  
  7775.     /* A profile in use cannot be dropped */
  7776.     if @usage_count = 0 
  7777.     begin
  7778.         RAISERROR(20065, 16, -1) -- Cannot drop profile, because it is in use.
  7779.         select @retstatus = 1
  7780.         goto UNDO 
  7781.     end
  7782.  
  7783.     BEGIN TRAN
  7784.  
  7785.     /*****
  7786.      * NOTE : If sp_drop_agent_parameter fails, the profile must not be 
  7787.      * deleted either 
  7788.      */
  7789.     EXECUTE @retcode = dbo.sp_drop_agent_parameter @profile_id = @profile_id,
  7790.                             @parameter_name = '%'
  7791.  
  7792.     IF @@ERROR <> 0 OR @retcode <> 0
  7793.         GOTO UNDO
  7794.             
  7795.     /* Delete all the entries in one go */
  7796.     DELETE msdb..MSagent_profiles 
  7797.     WHERE profile_id = @profile_id
  7798.  
  7799.     IF @@ERROR <> 0
  7800.         GOTO UNDO
  7801.  
  7802.     COMMIT TRAN
  7803.  
  7804.     close hCdistdbs
  7805.     deallocate hCdistdbs
  7806.  
  7807.     RETURN 0
  7808.  
  7809. UNDO:
  7810.     IF @@TRANCOUNT = 1
  7811.         ROLLBACK TRAN
  7812.     ELSE IF @@TRANCOUNT > 1 -- Sometimes we can get here when @@trancount = 0, so need to check explicitly.
  7813.         COMMIT TRAN
  7814.  
  7815.     close hCdistdbs
  7816.     deallocate hCdistdbs
  7817.  
  7818.  
  7819.     RETURN (1)
  7820. GO
  7821.  
  7822. raiserror('Creating procedure sp_help_agent_profile', 0,1)
  7823. go
  7824.  
  7825.  
  7826. -- View the row(s) of the MSagent_profiles table
  7827.  
  7828. create procedure sp_help_agent_profile(
  7829.     @agent_type     int = 0,
  7830.     @profile_id int = -1
  7831. )
  7832. as
  7833.     set nocount on
  7834.  
  7835.     declare @proc nvarchar(255)
  7836.  
  7837.     declare @snapshot_type      int
  7838.     declare @logreader_type     int
  7839.     declare @distribution_type  int
  7840.     declare @merge_type     int
  7841.     declare @qreader_type        int
  7842.  
  7843.     select @snapshot_type = 1
  7844.     select @logreader_type = 2
  7845.     select @distribution_type = 3
  7846.     select @merge_type = 4
  7847.     select @qreader_type = 9
  7848.  
  7849.     if @agent_type is null 
  7850.     BEGIN
  7851.         RAISERROR (14043, 16, -1, '@agent_type')
  7852.         RETURN (1)
  7853.     END
  7854.  
  7855.     if @profile_id is null
  7856.     BEGIN
  7857.         RAISERROR (14043, 16, -1, '@profile_id')
  7858.         RETURN (1)
  7859.     END
  7860.  
  7861.     if @agent_type not in (0, @snapshot_type, @logreader_type, @distribution_type, @merge_type, @qreader_type)
  7862.     BEGIN
  7863.         RAISERROR(20058, 16, -1)
  7864.         return (1)
  7865.     END
  7866.  
  7867.     select profile_id, profile_name, agent_type, type, description, def_profile
  7868.         from msdb..MSagent_profiles where
  7869.              (profile_id = @profile_id or @profile_id = -1) and
  7870.              (agent_type = @agent_type or @agent_type = 0)
  7871.         order by profile_id, profile_name
  7872.  
  7873. GO
  7874.  
  7875. raiserror('Creating procedure sp_help_agent_default', 0,1)
  7876. go
  7877.  
  7878. create procedure sp_help_agent_default (
  7879.     @profile_id     int OUTPUT, 
  7880.     @agent_type         int
  7881. )
  7882. as
  7883.     set nocount on
  7884.  
  7885.     if @agent_type not in (1, 2, 3, 4)
  7886.     BEGIN
  7887.         RAISERROR(20058, 16, -1)
  7888.         return (1)
  7889.     END
  7890.  
  7891.  
  7892.     select @profile_id = profile_id 
  7893.     from msdb..MSagent_profiles 
  7894.     where agent_type = @agent_type
  7895.     and def_profile = 1 
  7896. go
  7897.  
  7898. raiserror('Creating procedure sp_MSupdate_agenttype_default', 0,1)
  7899. go
  7900.  
  7901. -- Set a profile as a default for an agent_type
  7902. create procedure sp_MSupdate_agenttype_default(
  7903.     @profile_id int
  7904. )
  7905. as
  7906.     set nocount on
  7907.  
  7908.     declare @agent_type int
  7909.  
  7910.     /* This profile must have been defined for this agent type */
  7911.     if not exists ( select * from msdb..MSagent_profiles
  7912.             where profile_id = @profile_id )
  7913.     BEGIN
  7914.         RAISERROR (20066, 16, -1)   -- profile not defined
  7915.         RETURN (1)
  7916.     END
  7917.  
  7918.  
  7919.     select @agent_type = agent_type
  7920.     from msdb..MSagent_profiles 
  7921.     where profile_id = @profile_id
  7922.  
  7923.     BEGIN TRAN
  7924.  
  7925.     update msdb..MSagent_profiles
  7926.     set def_profile = 0
  7927.     where agent_type = @agent_type
  7928.     and def_profile = 1
  7929.  
  7930.     if @@error <> 0
  7931.         goto UNDO
  7932.  
  7933.     update msdb..MSagent_profiles
  7934.     set def_profile = 1
  7935.     where profile_id = @profile_id
  7936.  
  7937.     if @@error <> 0
  7938.         goto UNDO 
  7939.  
  7940.     COMMIT TRAN
  7941.  
  7942.     return 0
  7943.  
  7944. UNDO:
  7945.     if @@trancount = 1
  7946.         rollback tran
  7947.     else
  7948.         commit tran
  7949.  
  7950.     return 1 
  7951. GO
  7952.  
  7953.  
  7954. /*
  7955. ** Create stored procedures to generate default profile 
  7956. ** parameters for replication agents.
  7957. */
  7958.  
  7959. raiserror('Creating procedure sp_generate_agent_parameter', 0,1)
  7960. GO
  7961.  
  7962. create procedure sp_generate_agent_parameter(
  7963.     @profile_id     int,        -- Fixed profile id used
  7964.     @real_profile_id int = NULL -- Used by version upgrade 
  7965. )
  7966. as
  7967.     declare @retcode    int
  7968.  
  7969.     -- If real profile id is not passed in. Use the fix profile id.
  7970.     if @real_profile_id is NULL
  7971.         select @real_profile_id = @profile_id
  7972.  
  7973.         /* 
  7974.     ** Snapshot agent 
  7975.     */
  7976.     if (@profile_id = 1)
  7977.         begin
  7978.         exec @retcode = dbo.sp_add_agent_parameter 
  7979.                 @profile_id = @real_profile_id,
  7980.                 @parameter_name = '-BcpBatchSize',
  7981.                 @parameter_value = '100000'
  7982.         if (@retcode = 1 or @@ERROR <> 0)
  7983.             goto FAILURE
  7984.  
  7985.         exec @retcode = dbo.sp_add_agent_parameter 
  7986.                 @profile_id = @real_profile_id,
  7987.                 @parameter_name = '-HistoryVerboseLevel',
  7988.                 @parameter_value = '2'
  7989.         if (@retcode = 1 or @@ERROR <> 0)
  7990.             goto FAILURE
  7991.  
  7992.         exec @retcode = dbo.sp_add_agent_parameter 
  7993.                 @profile_id = @real_profile_id,
  7994.                 @parameter_name = '-LoginTimeout',
  7995.                 @parameter_value = '15'
  7996.         if (@retcode = 1 or @@ERROR <> 0)
  7997.             goto FAILURE
  7998.  
  7999.         exec @retcode = dbo.sp_add_agent_parameter 
  8000.                 @profile_id = @real_profile_id,
  8001.                 @parameter_name = '-MaxBcpThreads',
  8002.                 @parameter_value = '1'
  8003.         if (@retcode = 1 or @@ERROR <> 0)
  8004.             goto FAILURE
  8005.  
  8006.         exec @retcode = dbo.sp_add_agent_parameter 
  8007.                 @profile_id = @real_profile_id,
  8008.                 @parameter_name = '-QueryTimeout',
  8009.                 @parameter_value = '300'        -- 5 minutes 
  8010.         if (@retcode = 1 or @@ERROR <> 0)
  8011.             goto FAILURE
  8012.         end
  8013.  
  8014.         /* 
  8015.     ** Logreader agent 
  8016.     */
  8017.     else if (@profile_id = 2)
  8018.         begin
  8019.   
  8020.         exec @retcode = dbo.sp_add_agent_parameter 
  8021.                 @profile_id = @real_profile_id,
  8022.                 @parameter_name = '-HistoryVerboseLevel',
  8023.                 @parameter_value = '1'
  8024.         if (@retcode = 1 or @@ERROR <> 0)
  8025.             goto FAILURE
  8026.  
  8027.         exec @retcode = dbo.sp_add_agent_parameter 
  8028.                 @profile_id = @real_profile_id,
  8029.                 @parameter_name = '-LoginTimeout',
  8030.                 @parameter_value = '15'
  8031.         if (@retcode = 1 or @@ERROR <> 0)
  8032.             goto FAILURE
  8033.  
  8034.         exec @retcode = dbo.sp_add_agent_parameter 
  8035.                 @profile_id = @real_profile_id,
  8036.                 @parameter_name = '-PollingInterval',
  8037.                 @parameter_value = '10'
  8038.         if (@retcode = 1 or @@ERROR <> 0)
  8039.             goto FAILURE
  8040.  
  8041.         exec @retcode = dbo.sp_add_agent_parameter 
  8042.                 @profile_id = @real_profile_id,
  8043.                 @parameter_name = '-QueryTimeout',
  8044.                 @parameter_value = '300'        -- 5 minutes
  8045.         if (@retcode = 1 or @@ERROR <> 0)
  8046.             goto FAILURE
  8047.  
  8048.         exec @retcode = dbo.sp_add_agent_parameter 
  8049.                 @profile_id = @real_profile_id,
  8050.                 @parameter_name = '-ReadBatchSize',
  8051.                 @parameter_value = '500'
  8052.         if (@retcode = 1 or @@ERROR <> 0)
  8053.             goto FAILURE
  8054.  
  8055.         end
  8056.  
  8057.         /* 
  8058.     ** Logreader agent - Verbose History Profile
  8059.     */
  8060.     else if (@profile_id = 3)
  8061.         begin
  8062.  
  8063.         exec @retcode = dbo.sp_add_agent_parameter 
  8064.                 @profile_id = @real_profile_id,
  8065.                 @parameter_name = '-HistoryVerboseLevel',
  8066.                 @parameter_value = '2'
  8067.         if (@retcode = 1 or @@ERROR <> 0)
  8068.             goto FAILURE
  8069.  
  8070.         exec @retcode = dbo.sp_add_agent_parameter 
  8071.                 @profile_id = @real_profile_id,
  8072.                 @parameter_name = '-LoginTimeout',
  8073.                 @parameter_value = '15'
  8074.         if (@retcode = 1 or @@ERROR <> 0)
  8075.             goto FAILURE
  8076.  
  8077.         exec @retcode = dbo.sp_add_agent_parameter 
  8078.                 @profile_id = @real_profile_id,
  8079.                 @parameter_name = '-PollingInterval',
  8080.                 @parameter_value = '10'
  8081.         if (@retcode = 1 or @@ERROR <> 0)
  8082.             goto FAILURE
  8083.  
  8084.         exec @retcode = dbo.sp_add_agent_parameter 
  8085.                 @profile_id = @real_profile_id,
  8086.                 @parameter_name = '-QueryTimeout',
  8087.                 @parameter_value = '300'        -- 5 minutes
  8088.         if (@retcode = 1 or @@ERROR <> 0)
  8089.             goto FAILURE
  8090.  
  8091.         exec @retcode = dbo.sp_add_agent_parameter 
  8092.                 @profile_id = @real_profile_id,
  8093.                 @parameter_name = '-ReadBatchSize',
  8094.                 @parameter_value = '500'
  8095.         if (@retcode = 1 or @@ERROR <> 0)
  8096.             goto FAILURE
  8097.  
  8098.         end
  8099.  
  8100.         /* 
  8101.     ** Distribution agent 
  8102.     */
  8103.     else if (@profile_id = 4)
  8104.       begin
  8105.  
  8106.         exec @retcode = dbo.sp_add_agent_parameter 
  8107.                 @profile_id = @real_profile_id,
  8108.                 @parameter_name = '-BcpBatchSize',
  8109.                 @parameter_value = '100000'
  8110.         if (@retcode = 1 or @@ERROR <> 0)
  8111.             goto FAILURE
  8112.  
  8113.         exec @retcode = dbo.sp_add_agent_parameter 
  8114.                 @profile_id = @real_profile_id,
  8115.                 @parameter_name = '-CommitBatchSize',
  8116.                 @parameter_value = '100'
  8117.         if (@retcode = 1 or @@ERROR <> 0)
  8118.             goto FAILURE
  8119.  
  8120.         exec @retcode = dbo.sp_add_agent_parameter 
  8121.                 @profile_id = @real_profile_id,
  8122.                 @parameter_name = '-CommitBatchThreshold',
  8123.                 @parameter_value = '1000'
  8124.         if (@retcode = 1 or @@ERROR <> 0)
  8125.             goto FAILURE
  8126.  
  8127.         exec @retcode = dbo.sp_add_agent_parameter 
  8128.                 @profile_id = @real_profile_id,
  8129.                 @parameter_name = '-HistoryVerboseLevel',
  8130.                 @parameter_value = '1'
  8131.         if (@retcode = 1 or @@ERROR <> 0)
  8132.             goto FAILURE
  8133.  
  8134.         exec @retcode = dbo.sp_add_agent_parameter 
  8135.                 @profile_id = @real_profile_id,
  8136.                 @parameter_name = '-LoginTimeout',
  8137.                 @parameter_value = '15'
  8138.         if (@retcode = 1 or @@ERROR <> 0)
  8139.             goto FAILURE
  8140.  
  8141.         exec @retcode = dbo.sp_add_agent_parameter 
  8142.                 @profile_id = @real_profile_id,
  8143.                 @parameter_name = '-MaxBcpThreads',
  8144.                 @parameter_value = '1'
  8145.         if (@retcode = 1 or @@ERROR <> 0)
  8146.             goto FAILURE
  8147.  
  8148.         exec @retcode = dbo.sp_add_agent_parameter 
  8149.                 @profile_id = @real_profile_id,
  8150.                 @parameter_name = '-MaxDeliveredTransactions',
  8151.                 @parameter_value = '0'
  8152.         if (@retcode = 1 or @@ERROR <> 0)
  8153.             goto FAILURE
  8154.  
  8155.         exec @retcode = dbo.sp_add_agent_parameter 
  8156.                 @profile_id = @real_profile_id,
  8157.                 @parameter_name = '-PollingInterval',
  8158.                 @parameter_value = '10'
  8159.         if (@retcode = 1 or @@ERROR <> 0)
  8160.             goto FAILURE
  8161.  
  8162.         exec @retcode = dbo.sp_add_agent_parameter 
  8163.                 @profile_id = @real_profile_id,
  8164.                 @parameter_name = '-QueryTimeout',
  8165.                 @parameter_value = '300'        -- 5 minutes 
  8166.         if (@retcode = 1 or @@ERROR <> 0)
  8167.             goto FAILURE
  8168.  
  8169.         exec @retcode = dbo.sp_add_agent_parameter 
  8170.                 @profile_id = @real_profile_id,
  8171.                 @parameter_name = '-TransactionsPerHistory',
  8172.                 @parameter_value = '100'
  8173.         if (@retcode = 1 or @@ERROR <> 0)
  8174.             goto FAILURE
  8175.  
  8176.         exec @retcode = dbo.sp_add_agent_parameter 
  8177.                 @profile_id = @real_profile_id,
  8178.                 @parameter_name = '-SkipErrors',
  8179.                 @parameter_value = ''
  8180.         if (@retcode = 1 or @@ERROR <> 0)
  8181.             goto FAILURE
  8182.  
  8183.         exec @retcode = dbo.sp_add_agent_parameter
  8184.                 @profile_id = @real_profile_id,
  8185.                 @parameter_name = '-KeepAliveMessageInterval',
  8186.                 @parameter_value = '300'
  8187.         if (@retcode = 1 or @@ERROR <> 0)
  8188.             goto FAILURE
  8189.  
  8190.         end
  8191.  
  8192.     /*
  8193.     ** Distribution Agent Verbose History Profile
  8194.     */
  8195.     else if (@profile_id = 5)
  8196.       begin
  8197.  
  8198.         exec @retcode = dbo.sp_add_agent_parameter 
  8199.                 @profile_id = @real_profile_id,
  8200.                 @parameter_name = '-BcpBatchSize',
  8201.                 @parameter_value = '100000'
  8202.         if (@retcode = 1 or @@ERROR <> 0)
  8203.             goto FAILURE
  8204.  
  8205.         exec @retcode = dbo.sp_add_agent_parameter 
  8206.                 @profile_id = @real_profile_id,
  8207.                 @parameter_name = '-CommitBatchSize',
  8208.                 @parameter_value = '100'
  8209.         if (@retcode = 1 or @@ERROR <> 0)
  8210.             goto FAILURE
  8211.  
  8212.         exec @retcode = dbo.sp_add_agent_parameter 
  8213.                 @profile_id = @real_profile_id,
  8214.                 @parameter_name = '-CommitBatchThreshold',
  8215.                 @parameter_value = '1000'
  8216.         if (@retcode = 1 or @@ERROR <> 0)
  8217.             goto FAILURE
  8218.  
  8219.         exec @retcode = dbo.sp_add_agent_parameter 
  8220.                 @profile_id = @real_profile_id,
  8221.                 @parameter_name = '-HistoryVerboseLevel',
  8222.                 @parameter_value = '2'
  8223.         if (@retcode = 1 or @@ERROR <> 0)
  8224.             goto FAILURE
  8225.  
  8226.         exec @retcode = dbo.sp_add_agent_parameter 
  8227.                 @profile_id = @real_profile_id,
  8228.                 @parameter_name = '-LoginTimeout',
  8229.                 @parameter_value = '15'
  8230.         if (@retcode = 1 or @@ERROR <> 0)
  8231.             goto FAILURE
  8232.  
  8233.         exec @retcode = dbo.sp_add_agent_parameter 
  8234.                 @profile_id = @real_profile_id,
  8235.                 @parameter_name = '-MaxBcpThreads',
  8236.                 @parameter_value = '1'
  8237.         if (@retcode = 1 or @@ERROR <> 0)
  8238.             goto FAILURE
  8239.  
  8240.         exec @retcode = dbo.sp_add_agent_parameter 
  8241.                 @profile_id = @real_profile_id,
  8242.                 @parameter_name = '-MaxDeliveredTransactions',
  8243.                 @parameter_value = '0'
  8244.         if (@retcode = 1 or @@ERROR <> 0)
  8245.             goto FAILURE
  8246.  
  8247.         exec @retcode = dbo.sp_add_agent_parameter 
  8248.                 @profile_id = @real_profile_id,
  8249.                 @parameter_name = '-PollingInterval',
  8250.                 @parameter_value = '10'
  8251.         if (@retcode = 1 or @@ERROR <> 0)
  8252.             goto FAILURE
  8253.  
  8254.         exec @retcode = dbo.sp_add_agent_parameter 
  8255.                 @profile_id = @real_profile_id,
  8256.                 @parameter_name = '-QueryTimeout',
  8257.                 @parameter_value = '300'        -- 5 minutes 
  8258.         if (@retcode = 1 or @@ERROR <> 0)
  8259.             goto FAILURE
  8260.  
  8261.         exec @retcode = dbo.sp_add_agent_parameter 
  8262.                 @profile_id = @real_profile_id,
  8263.                 @parameter_name = '-TransactionsPerHistory',
  8264.                 @parameter_value = '100'
  8265.         if (@retcode = 1 or @@ERROR <> 0)
  8266.             goto FAILURE
  8267.  
  8268.         exec @retcode = dbo.sp_add_agent_parameter 
  8269.                 @profile_id = @real_profile_id,
  8270.                 @parameter_name = '-SkipErrors',
  8271.                 @parameter_value = ''
  8272.         if (@retcode = 1 or @@ERROR <> 0)
  8273.             goto FAILURE
  8274.  
  8275.         exec @retcode = dbo.sp_add_agent_parameter
  8276.                 @profile_id = @real_profile_id,
  8277.                 @parameter_name = '-KeepAliveMessageInterval',
  8278.                 @parameter_value = '300'
  8279.         if (@retcode = 1 or @@ERROR <> 0)
  8280.             goto FAILURE
  8281.  
  8282.         end
  8283.     /* 
  8284.     ** Merge agent : Default profile for well connected scenarios 
  8285.     */
  8286.     else if (@profile_id = 6)
  8287.         begin
  8288.         exec @retcode = dbo.sp_add_agent_parameter 
  8289.                 @profile_id = @real_profile_id,
  8290.                 @parameter_name = '-PollingInterval',
  8291.                 @parameter_value = '60'
  8292.         if (@retcode = 1 or @@ERROR <> 0)
  8293.             goto FAILURE
  8294.  
  8295.         exec @retcode = dbo.sp_add_agent_parameter 
  8296.                 @profile_id = @real_profile_id,
  8297.                 @parameter_name = '-StartQueueTimeout',
  8298.                 @parameter_value = '0'
  8299.         if (@retcode = 1 or @@ERROR <> 0)
  8300.             goto FAILURE
  8301.  
  8302.         exec @retcode = dbo.sp_add_agent_parameter 
  8303.                 @profile_id = @real_profile_id,
  8304.                 @parameter_name = '-MaxDownloadChanges',
  8305.                 @parameter_value = '0'
  8306.         if (@retcode = 1 or @@ERROR <> 0)
  8307.             goto FAILURE
  8308.  
  8309.         exec @retcode = dbo.sp_add_agent_parameter 
  8310.                 @profile_id = @real_profile_id,
  8311.                 @parameter_name = '-MaxUploadChanges',
  8312.                 @parameter_value = '0'
  8313.         if (@retcode = 1 or @@ERROR <> 0)
  8314.             goto FAILURE
  8315.  
  8316.         exec @retcode = dbo.sp_add_agent_parameter 
  8317.                 @profile_id = @real_profile_id,
  8318.                 @parameter_name = '-ValidateInterval',
  8319.                 @parameter_value = '60'
  8320.         if (@retcode = 1 or @@ERROR <> 0)
  8321.             goto FAILURE
  8322.  
  8323.         exec @retcode = dbo.sp_add_agent_parameter 
  8324.                 @profile_id = @real_profile_id,
  8325.                 @parameter_name = '-LoginTimeout',
  8326.                 @parameter_value = '15'
  8327.         if (@retcode = 1 or @@ERROR <> 0)
  8328.             goto FAILURE
  8329.  
  8330.         exec @retcode = dbo.sp_add_agent_parameter 
  8331.                 @profile_id = @real_profile_id,
  8332.                 @parameter_name = '-QueryTimeout',
  8333.                 @parameter_value = '300'        
  8334.         if (@retcode = 1 or @@ERROR <> 0)
  8335.             goto FAILURE
  8336.  
  8337.         exec @retcode = dbo.sp_add_agent_parameter 
  8338.                 @profile_id = @real_profile_id,
  8339.                 @parameter_name = '-UploadGenerationsPerBatch',
  8340.                 @parameter_value = '100'
  8341.         if (@retcode = 1 or @@ERROR <> 0)
  8342.             goto FAILURE
  8343.  
  8344.         exec @retcode = dbo.sp_add_agent_parameter 
  8345.                 @profile_id = @real_profile_id,
  8346.                 @parameter_name = '-DownloadGenerationsPerBatch',
  8347.                 @parameter_value = '100'
  8348.         if (@retcode = 1 or @@ERROR <> 0)
  8349.             goto FAILURE
  8350.  
  8351.         exec @retcode = dbo.sp_add_agent_parameter 
  8352.                 @profile_id = @real_profile_id,
  8353.                 @parameter_name = '-UploadReadChangesPerBatch',
  8354.                 @parameter_value = '100'
  8355.         if (@retcode = 1 or @@ERROR <> 0)
  8356.             goto FAILURE
  8357.  
  8358.         exec @retcode = dbo.sp_add_agent_parameter 
  8359.                 @profile_id = @real_profile_id,
  8360.                 @parameter_name = '-DownloadReadChangesPerBatch',
  8361.                 @parameter_value = '100'
  8362.         if (@retcode = 1 or @@ERROR <> 0)
  8363.             goto FAILURE
  8364.  
  8365.         exec @retcode = dbo.sp_add_agent_parameter 
  8366.                 @profile_id = @real_profile_id,
  8367.                 @parameter_name = '-UploadWriteChangesPerBatch',
  8368.                 @parameter_value = '100'
  8369.         if (@retcode = 1 or @@ERROR <> 0)
  8370.             goto FAILURE
  8371.  
  8372.         exec @retcode = dbo.sp_add_agent_parameter 
  8373.                 @profile_id = @real_profile_id,
  8374.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  8375.                 @parameter_value = '100'
  8376.         if (@retcode = 1 or @@ERROR <> 0)
  8377.             goto FAILURE
  8378.  
  8379.         exec @retcode = dbo.sp_add_agent_parameter 
  8380.                 @profile_id = @real_profile_id,
  8381.                 @parameter_name = '-Validate',
  8382.                 @parameter_value = '0'
  8383.         if (@retcode = 1 or @@ERROR <> 0)
  8384.             goto FAILURE
  8385.  
  8386.         exec @retcode = dbo.sp_add_agent_parameter 
  8387.                 @profile_id = @real_profile_id,
  8388.                 @parameter_name = '-FastRowCount',
  8389.                 @parameter_value = '1'
  8390.         if (@retcode = 1 or @@ERROR <> 0)
  8391.             goto FAILURE
  8392.         
  8393.         exec @retcode = dbo.sp_add_agent_parameter 
  8394.                 @profile_id = @real_profile_id,
  8395.                 @parameter_name = '-HistoryVerboseLevel',
  8396.                 @parameter_value = '1'
  8397.         if (@retcode = 1 or @@ERROR <> 0)
  8398.             goto FAILURE
  8399.  
  8400.         exec @retcode = dbo.sp_add_agent_parameter 
  8401.                 @profile_id = @real_profile_id,
  8402.                 @parameter_name = '-ChangesPerHistory',
  8403.                 @parameter_value = '100'
  8404.         if (@retcode = 1 or @@ERROR <> 0)
  8405.             goto FAILURE
  8406.  
  8407.         exec @retcode = dbo.sp_add_agent_parameter 
  8408.                 @profile_id = @real_profile_id,
  8409.                 @parameter_name = '-BcpBatchSize',
  8410.                 @parameter_value = '100000'
  8411.         if (@retcode = 1 or @@ERROR <> 0)
  8412.             goto FAILURE
  8413.  
  8414.         exec @retcode = dbo.sp_add_agent_parameter 
  8415.                 @profile_id = @real_profile_id,
  8416.                 @parameter_name = '-NumDeadlockRetries',
  8417.                 @parameter_value = '5'
  8418.         if (@retcode = 1 or @@ERROR <> 0)
  8419.             goto FAILURE
  8420.  
  8421.         exec @retcode = dbo.sp_add_agent_parameter 
  8422.                 @profile_id = @real_profile_id,
  8423.                 @parameter_name = '-SrcThreads',
  8424.                 @parameter_value = '3'
  8425.         if (@retcode = 1 or @@ERROR <> 0)
  8426.             goto FAILURE
  8427.  
  8428.         exec @retcode = dbo.sp_add_agent_parameter 
  8429.                 @profile_id = @real_profile_id,
  8430.                 @parameter_name = '-DestThreads',
  8431.                 @parameter_value = '4'
  8432.         if (@retcode = 1 or @@ERROR <> 0)
  8433.             goto FAILURE
  8434.  
  8435.         exec @retcode = dbo.sp_add_agent_parameter
  8436.                 @profile_id = @real_profile_id,
  8437.                 @parameter_name = '-KeepAliveMessageInterval',
  8438.                 @parameter_value = '300'
  8439.         if (@retcode = 1 or @@ERROR <> 0)
  8440.             goto FAILURE
  8441.  
  8442.         exec @retcode = dbo.sp_add_agent_parameter
  8443.                 @profile_id = @real_profile_id,
  8444.                 @parameter_name = '-MetadataRetentionCleanup',
  8445.                 @parameter_value = '1'
  8446.         if (@retcode = 1 or @@ERROR <> 0)
  8447.             goto FAILURE
  8448.  
  8449.         end
  8450.  
  8451.     /* 
  8452.     ** Merge agent : Non default profile for disconnected scenarios ( unreliable link ) 
  8453.     */
  8454.     else if (@profile_id = 7)
  8455.         begin
  8456.  
  8457.         exec @retcode = dbo.sp_add_agent_parameter 
  8458.                 @profile_id = @real_profile_id,
  8459.                 @parameter_name = '-PollingInterval',
  8460.                 @parameter_value = '60'
  8461.         if (@retcode = 1 or @@ERROR <> 0)
  8462.             goto FAILURE
  8463.  
  8464.         exec @retcode = dbo.sp_add_agent_parameter 
  8465.                 @profile_id = @real_profile_id,
  8466.                 @parameter_name = '-StartQueueTimeout',
  8467.                 @parameter_value = '0'
  8468.         if (@retcode = 1 or @@ERROR <> 0)
  8469.             goto FAILURE
  8470.  
  8471.         exec @retcode = dbo.sp_add_agent_parameter 
  8472.                 @profile_id = @real_profile_id,
  8473.                 @parameter_name = '-MaxDownloadChanges',
  8474.                 @parameter_value = '0'
  8475.         if (@retcode = 1 or @@ERROR <> 0)
  8476.             goto FAILURE
  8477.  
  8478.         exec @retcode = dbo.sp_add_agent_parameter 
  8479.                 @profile_id = @real_profile_id,
  8480.                 @parameter_name = '-MaxUploadChanges',
  8481.                 @parameter_value = '0'
  8482.         if (@retcode = 1 or @@ERROR <> 0)
  8483.             goto FAILURE
  8484.             
  8485.         exec @retcode = dbo.sp_add_agent_parameter 
  8486.                 @profile_id = @real_profile_id,
  8487.                 @parameter_name = '-ValidateInterval',
  8488.                 @parameter_value = '60'
  8489.         if (@retcode = 1 or @@ERROR <> 0)
  8490.             goto FAILURE
  8491.  
  8492.         exec @retcode = dbo.sp_add_agent_parameter 
  8493.                 @profile_id = @real_profile_id,
  8494.                 @parameter_name = '-LoginTimeout',
  8495.                 @parameter_value = '15'
  8496.         if (@retcode = 1 or @@ERROR <> 0)
  8497.             goto FAILURE
  8498.  
  8499.         exec @retcode = dbo.sp_add_agent_parameter 
  8500.                 @profile_id = @real_profile_id,
  8501.                 @parameter_name = '-QueryTimeout',
  8502.                 @parameter_value = '300'
  8503.         if (@retcode = 1 or @@ERROR <> 0)
  8504.             goto FAILURE
  8505.  
  8506.         exec @retcode = dbo.sp_add_agent_parameter 
  8507.                 @profile_id = @real_profile_id,
  8508.                 @parameter_name = '-UploadGenerationsPerBatch',
  8509.                 @parameter_value = '20'
  8510.         if (@retcode = 1 or @@ERROR <> 0)
  8511.             goto FAILURE
  8512.  
  8513.         exec @retcode = dbo.sp_add_agent_parameter 
  8514.                 @profile_id = @real_profile_id,
  8515.                 @parameter_name = '-DownloadGenerationsPerBatch',
  8516.                 @parameter_value = '100'
  8517.         if (@retcode = 1 or @@ERROR <> 0)
  8518.             goto FAILURE
  8519.  
  8520.         exec @retcode = dbo.sp_add_agent_parameter 
  8521.                 @profile_id = @real_profile_id,
  8522.                 @parameter_name = '-UploadReadChangesPerBatch',
  8523.                 @parameter_value = '100'
  8524.         if (@retcode = 1 or @@ERROR <> 0)
  8525.             goto FAILURE
  8526.  
  8527.         exec @retcode = dbo.sp_add_agent_parameter 
  8528.                 @profile_id = @real_profile_id,
  8529.                 @parameter_name = '-DownloadReadChangesPerBatch',
  8530.                 @parameter_value = '100'
  8531.         if (@retcode = 1 or @@ERROR <> 0)
  8532.             goto FAILURE
  8533.  
  8534.         exec @retcode = dbo.sp_add_agent_parameter 
  8535.                 @profile_id = @real_profile_id,
  8536.                 @parameter_name = '-UploadWriteChangesPerBatch',
  8537.                 @parameter_value = '100'
  8538.         if (@retcode = 1 or @@ERROR <> 0)
  8539.             goto FAILURE
  8540.  
  8541.         exec @retcode = dbo.sp_add_agent_parameter 
  8542.                 @profile_id = @real_profile_id,
  8543.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  8544.                 @parameter_value = '100'
  8545.         if (@retcode = 1 or @@ERROR <> 0)
  8546.             goto FAILURE
  8547.  
  8548.         exec @retcode = dbo.sp_add_agent_parameter 
  8549.                 @profile_id = @real_profile_id,
  8550.                 @parameter_name = '-Validate',
  8551.                 @parameter_value = '0'
  8552.         if (@retcode = 1 or @@ERROR <> 0)
  8553.             goto FAILURE
  8554.         
  8555.         exec @retcode = dbo.sp_add_agent_parameter 
  8556.                 @profile_id = @real_profile_id,
  8557.                 @parameter_name = '-FastRowCount',
  8558.                 @parameter_value = '1'
  8559.         if (@retcode = 1 or @@ERROR <> 0)
  8560.             goto FAILURE
  8561.  
  8562.         exec @retcode = dbo.sp_add_agent_parameter 
  8563.                 @profile_id = @real_profile_id,
  8564.                 @parameter_name = '-HistoryVerboseLevel',
  8565.                 @parameter_value = '1'
  8566.         if (@retcode = 1 or @@ERROR <> 0)
  8567.             goto FAILURE
  8568.  
  8569.         exec @retcode = dbo.sp_add_agent_parameter 
  8570.                 @profile_id = @real_profile_id,
  8571.                 @parameter_name = '-ChangesPerHistory',
  8572.                 @parameter_value = '100'
  8573.         if (@retcode = 1 or @@ERROR <> 0)
  8574.             goto FAILURE
  8575.  
  8576.         exec @retcode = dbo.sp_add_agent_parameter 
  8577.                 @profile_id = @real_profile_id,
  8578.                 @parameter_name = '-BcpBatchSize',
  8579.                 @parameter_value = '100000'
  8580.         if (@retcode = 1 or @@ERROR <> 0)
  8581.             goto FAILURE
  8582.  
  8583.         exec @retcode = dbo.sp_add_agent_parameter 
  8584.                 @profile_id = @real_profile_id,
  8585.                 @parameter_name = '-KeepAliveMessageInterval',
  8586.                 @parameter_value = '300'
  8587.         if (@retcode = 1 or @@ERROR <> 0)
  8588.             goto FAILURE
  8589.  
  8590.         exec @retcode = dbo.sp_add_agent_parameter 
  8591.                 @profile_id = @real_profile_id,
  8592.                 @parameter_name = '-NumDeadlockRetries',
  8593.                 @parameter_value = '5'
  8594.         if (@retcode = 1 or @@ERROR <> 0)
  8595.             goto FAILURE
  8596.  
  8597.         exec @retcode = dbo.sp_add_agent_parameter 
  8598.                 @profile_id = @real_profile_id,
  8599.                 @parameter_name = '-SrcThreads',
  8600.                 @parameter_value = '3'
  8601.         if (@retcode = 1 or @@ERROR <> 0)
  8602.             goto FAILURE
  8603.  
  8604.         exec @retcode = dbo.sp_add_agent_parameter 
  8605.                 @profile_id = @real_profile_id,
  8606.                 @parameter_name = '-DestThreads',
  8607.                 @parameter_value = '4'
  8608.         if (@retcode = 1 or @@ERROR <> 0)
  8609.             goto FAILURE
  8610.  
  8611.         exec @retcode = dbo.sp_add_agent_parameter
  8612.                 @profile_id = @real_profile_id,
  8613.                 @parameter_name = '-MetadataRetentionCleanup',
  8614.                 @parameter_value = '1'
  8615.         if (@retcode = 1 or @@ERROR <> 0)
  8616.             goto FAILURE
  8617.  
  8618.         end
  8619.  
  8620.         /* 
  8621.     ** Merge agent : Default profile for well connected scenarios - Verbose history logging 
  8622.     */
  8623.     else if (@profile_id = 8)
  8624.         begin
  8625.         exec @retcode = dbo.sp_add_agent_parameter 
  8626.                 @profile_id = @real_profile_id,
  8627.                 @parameter_name = '-PollingInterval',
  8628.                 @parameter_value = '60'
  8629.         if (@retcode = 1 or @@ERROR <> 0)
  8630.             goto FAILURE
  8631.             
  8632.         exec @retcode = dbo.sp_add_agent_parameter 
  8633.                 @profile_id = @real_profile_id,
  8634.                 @parameter_name = '-StartQueueTimeout',
  8635.                 @parameter_value = '0'
  8636.         if (@retcode = 1 or @@ERROR <> 0)
  8637.             goto FAILURE
  8638.             
  8639.         exec @retcode = dbo.sp_add_agent_parameter 
  8640.                 @profile_id = @real_profile_id,
  8641.                 @parameter_name = '-MaxDownloadChanges',
  8642.                 @parameter_value = '0'
  8643.         if (@retcode = 1 or @@ERROR <> 0)
  8644.             goto FAILURE
  8645.  
  8646.         exec @retcode = dbo.sp_add_agent_parameter 
  8647.                 @profile_id = @real_profile_id,
  8648.                 @parameter_name = '-MaxUploadChanges',
  8649.                 @parameter_value = '0'
  8650.         if (@retcode = 1 or @@ERROR <> 0)
  8651.             goto FAILURE
  8652.  
  8653.         exec @retcode = dbo.sp_add_agent_parameter 
  8654.                 @profile_id = @real_profile_id,
  8655.                 @parameter_name = '-ValidateInterval',
  8656.                 @parameter_value = '60'
  8657.         if (@retcode = 1 or @@ERROR <> 0)
  8658.             goto FAILURE
  8659.  
  8660.         exec @retcode = dbo.sp_add_agent_parameter 
  8661.                 @profile_id = @real_profile_id,
  8662.                 @parameter_name = '-LoginTimeout',
  8663.                 @parameter_value = '15'
  8664.         if (@retcode = 1 or @@ERROR <> 0)
  8665.             goto FAILURE
  8666.  
  8667.         exec @retcode = dbo.sp_add_agent_parameter 
  8668.                 @profile_id = @real_profile_id,
  8669.                 @parameter_name = '-QueryTimeout',
  8670.                 @parameter_value = '300'        
  8671.         if (@retcode = 1 or @@ERROR <> 0)
  8672.             goto FAILURE
  8673.  
  8674.         exec @retcode = dbo.sp_add_agent_parameter 
  8675.                 @profile_id = @real_profile_id,
  8676.                 @parameter_name = '-UploadGenerationsPerBatch',
  8677.                 @parameter_value = '100'
  8678.         if (@retcode = 1 or @@ERROR <> 0)
  8679.             goto FAILURE
  8680.  
  8681.         exec @retcode = dbo.sp_add_agent_parameter 
  8682.                 @profile_id = @real_profile_id,
  8683.                 @parameter_name = '-DownloadGenerationsPerBatch',
  8684.                 @parameter_value = '100'
  8685.         if (@retcode = 1 or @@ERROR <> 0)
  8686.             goto FAILURE
  8687.  
  8688.         exec @retcode = dbo.sp_add_agent_parameter 
  8689.                 @profile_id = @real_profile_id,
  8690.                 @parameter_name = '-UploadReadChangesPerBatch',
  8691.                 @parameter_value = '100'
  8692.         if (@retcode = 1 or @@ERROR <> 0)
  8693.             goto FAILURE
  8694.  
  8695.         exec @retcode = dbo.sp_add_agent_parameter 
  8696.                 @profile_id = @real_profile_id,
  8697.                 @parameter_name = '-DownloadReadChangesPerBatch',
  8698.                 @parameter_value = '100'
  8699.         if (@retcode = 1 or @@ERROR <> 0)
  8700.             goto FAILURE
  8701.  
  8702.         exec @retcode = dbo.sp_add_agent_parameter 
  8703.                 @profile_id = @real_profile_id,
  8704.                 @parameter_name = '-UploadWriteChangesPerBatch',
  8705.                 @parameter_value = '100'
  8706.         if (@retcode = 1 or @@ERROR <> 0)
  8707.             goto FAILURE
  8708.  
  8709.         exec @retcode = dbo.sp_add_agent_parameter 
  8710.                 @profile_id = @real_profile_id,
  8711.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  8712.                 @parameter_value = '100'
  8713.         if (@retcode = 1 or @@ERROR <> 0)
  8714.             goto FAILURE
  8715.  
  8716.         exec @retcode = dbo.sp_add_agent_parameter 
  8717.                 @profile_id = @real_profile_id,
  8718.                 @parameter_name = '-Validate',
  8719.                 @parameter_value = '0'
  8720.         if (@retcode = 1 or @@ERROR <> 0)
  8721.             goto FAILURE
  8722.  
  8723.         exec @retcode = dbo.sp_add_agent_parameter 
  8724.                 @profile_id = @real_profile_id,
  8725.                 @parameter_name = '-FastRowCount',
  8726.                 @parameter_value = '1'
  8727.         if (@retcode = 1 or @@ERROR <> 0)
  8728.             goto FAILURE
  8729.         
  8730.         exec @retcode = dbo.sp_add_agent_parameter 
  8731.                 @profile_id = @real_profile_id,
  8732.                 @parameter_name = '-HistoryVerboseLevel',
  8733.                 @parameter_value = '3'
  8734.         if (@retcode = 1 or @@ERROR <> 0)
  8735.             goto FAILURE
  8736.  
  8737.         exec @retcode = dbo.sp_add_agent_parameter 
  8738.                 @profile_id = @real_profile_id,
  8739.                 @parameter_name = '-ChangesPerHistory',
  8740.                 @parameter_value = '50'
  8741.         if (@retcode = 1 or @@ERROR <> 0)
  8742.             goto FAILURE
  8743.  
  8744.         exec @retcode = dbo.sp_add_agent_parameter 
  8745.                 @profile_id = @real_profile_id,
  8746.                 @parameter_name = '-BcpBatchSize',
  8747.                 @parameter_value = '100000'
  8748.         if (@retcode = 1 or @@ERROR <> 0)
  8749.             goto FAILURE
  8750.  
  8751.         exec @retcode = dbo.sp_add_agent_parameter 
  8752.                 @profile_id = @real_profile_id,
  8753.                 @parameter_name = '-NumDeadlockRetries',
  8754.                 @parameter_value = '5'
  8755.         if (@retcode = 1 or @@ERROR <> 0)
  8756.             goto FAILURE
  8757.  
  8758.         exec @retcode = dbo.sp_add_agent_parameter 
  8759.                 @profile_id = @real_profile_id,
  8760.                 @parameter_name = '-SrcThreads',
  8761.                 @parameter_value = '3'
  8762.         if (@retcode = 1 or @@ERROR <> 0)
  8763.             goto FAILURE
  8764.  
  8765.         exec @retcode = dbo.sp_add_agent_parameter 
  8766.                 @profile_id = @real_profile_id,
  8767.                 @parameter_name = '-DestThreads',
  8768.                 @parameter_value = '4'
  8769.         if (@retcode = 1 or @@ERROR <> 0)
  8770.             goto FAILURE
  8771.  
  8772.         exec @retcode = dbo.sp_add_agent_parameter
  8773.                 @profile_id = @real_profile_id,
  8774.                 @parameter_name = '-KeepAliveMessageInterval',
  8775.                 @parameter_value = '300'
  8776.         if (@retcode = 1 or @@ERROR <> 0)
  8777.             goto FAILURE
  8778.  
  8779.         exec @retcode = dbo.sp_add_agent_parameter
  8780.                 @profile_id = @real_profile_id,
  8781.                 @parameter_name = '-MetadataRetentionCleanup',
  8782.                 @parameter_value = '1'
  8783.         if (@retcode = 1 or @@ERROR <> 0)
  8784.             goto FAILURE
  8785.  
  8786.         end
  8787.  
  8788.     /* 
  8789.     ** Merge agent : (Synchronization Manager Profile)
  8790.     */
  8791.     else if (@profile_id = 9)
  8792.         begin
  8793.         exec @retcode = dbo.sp_add_agent_parameter 
  8794.                 @profile_id = @real_profile_id,
  8795.                 @parameter_name = '-PollingInterval',
  8796.                 @parameter_value = '60'
  8797.         if (@retcode = 1 or @@ERROR <> 0)
  8798.             goto FAILURE
  8799.  
  8800.         exec @retcode = dbo.sp_add_agent_parameter 
  8801.                 @profile_id = @real_profile_id,
  8802.                 @parameter_name = '-StartQueueTimeout',
  8803.                 @parameter_value = '0'
  8804.         if (@retcode = 1 or @@ERROR <> 0)
  8805.             goto FAILURE
  8806.  
  8807.         exec @retcode = dbo.sp_add_agent_parameter 
  8808.                 @profile_id = @real_profile_id,
  8809.                 @parameter_name = '-MaxDownloadChanges',
  8810.                 @parameter_value = '0'
  8811.         if (@retcode = 1 or @@ERROR <> 0)
  8812.             goto FAILURE
  8813.  
  8814.         exec @retcode = dbo.sp_add_agent_parameter 
  8815.                 @profile_id = @real_profile_id,
  8816.                 @parameter_name = '-MaxUploadChanges',
  8817.                 @parameter_value = '0'
  8818.         if (@retcode = 1 or @@ERROR <> 0)
  8819.             goto FAILURE
  8820.             
  8821.         exec @retcode = dbo.sp_add_agent_parameter 
  8822.                 @profile_id = @real_profile_id,
  8823.                 @parameter_name = '-ValidateInterval',
  8824.                 @parameter_value = '60'
  8825.         if (@retcode = 1 or @@ERROR <> 0)
  8826.             goto FAILURE
  8827.  
  8828.         exec @retcode = dbo.sp_add_agent_parameter 
  8829.                 @profile_id = @real_profile_id,
  8830.                 @parameter_name = '-LoginTimeout',
  8831.                 @parameter_value = '15'
  8832.         if (@retcode = 1 or @@ERROR <> 0)
  8833.             goto FAILURE
  8834.  
  8835.         exec @retcode = dbo.sp_add_agent_parameter 
  8836.                 @profile_id = @real_profile_id,
  8837.                 @parameter_name = '-QueryTimeout',
  8838.                 @parameter_value = '300'
  8839.         if (@retcode = 1 or @@ERROR <> 0)
  8840.             goto FAILURE
  8841.  
  8842.         exec @retcode = dbo.sp_add_agent_parameter 
  8843.                 @profile_id = @real_profile_id,
  8844.                 @parameter_name = '-UploadGenerationsPerBatch',
  8845.                 @parameter_value = '100'
  8846.         if (@retcode = 1 or @@ERROR <> 0)
  8847.             goto FAILURE
  8848.  
  8849.         exec @retcode = dbo.sp_add_agent_parameter 
  8850.                 @profile_id = @real_profile_id,
  8851.                 @parameter_name = '-DownloadGenerationsPerBatch',
  8852.                 @parameter_value = '100'
  8853.         if (@retcode = 1 or @@ERROR <> 0)
  8854.             goto FAILURE
  8855.  
  8856.         exec @retcode = dbo.sp_add_agent_parameter 
  8857.                 @profile_id = @real_profile_id,
  8858.                 @parameter_name = '-UploadReadChangesPerBatch',
  8859.                 @parameter_value = '100'
  8860.         if (@retcode = 1 or @@ERROR <> 0)
  8861.             goto FAILURE
  8862.  
  8863.         exec @retcode = dbo.sp_add_agent_parameter 
  8864.                 @profile_id = @real_profile_id,
  8865.                 @parameter_name = '-DownloadReadChangesPerBatch',
  8866.                 @parameter_value = '100'
  8867.         if (@retcode = 1 or @@ERROR <> 0)
  8868.             goto FAILURE
  8869.  
  8870.         exec @retcode = dbo.sp_add_agent_parameter 
  8871.                 @profile_id = @real_profile_id,
  8872.                 @parameter_name = '-UploadWriteChangesPerBatch',
  8873.                 @parameter_value = '100'
  8874.         if (@retcode = 1 or @@ERROR <> 0)
  8875.             goto FAILURE
  8876.  
  8877.         exec @retcode = dbo.sp_add_agent_parameter 
  8878.                 @profile_id = @real_profile_id,
  8879.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  8880.                 @parameter_value = '100'
  8881.         if (@retcode = 1 or @@ERROR <> 0)
  8882.             goto FAILURE
  8883.  
  8884.         exec @retcode = dbo.sp_add_agent_parameter 
  8885.                 @profile_id = @real_profile_id,
  8886.                 @parameter_name = '-Validate',
  8887.                 @parameter_value = '0'
  8888.         if (@retcode = 1 or @@ERROR <> 0)
  8889.             goto FAILURE
  8890.         
  8891.         exec @retcode = dbo.sp_add_agent_parameter 
  8892.                 @profile_id = @real_profile_id,
  8893.                 @parameter_name = '-FastRowCount',
  8894.                 @parameter_value = '1'
  8895.         if (@retcode = 1 or @@ERROR <> 0)
  8896.             goto FAILURE
  8897.  
  8898.         exec @retcode = dbo.sp_add_agent_parameter 
  8899.                 @profile_id = @real_profile_id,
  8900.                 @parameter_name = '-HistoryVerboseLevel',
  8901.                 @parameter_value = '1'
  8902.         if (@retcode = 1 or @@ERROR <> 0)
  8903.             goto FAILURE
  8904.  
  8905.         exec @retcode = dbo.sp_add_agent_parameter 
  8906.                 @profile_id = @real_profile_id,
  8907.                 @parameter_name = '-ChangesPerHistory',
  8908.                 @parameter_value = '50'
  8909.         if (@retcode = 1 or @@ERROR <> 0)
  8910.             goto FAILURE
  8911.  
  8912.         exec @retcode = dbo.sp_add_agent_parameter 
  8913.                 @profile_id = @real_profile_id,
  8914.                 @parameter_name = '-BcpBatchSize',
  8915.                 @parameter_value = '1000'
  8916.         if (@retcode = 1 or @@ERROR <> 0)
  8917.             goto FAILURE
  8918.  
  8919.         exec @retcode = dbo.sp_add_agent_parameter 
  8920.                 @profile_id = @real_profile_id,
  8921.                 @parameter_name = '-NumDeadlockRetries',
  8922.                 @parameter_value = '5'
  8923.         if (@retcode = 1 or @@ERROR <> 0)
  8924.             goto FAILURE
  8925.  
  8926.         exec @retcode = dbo.sp_add_agent_parameter 
  8927.                 @profile_id = @real_profile_id,
  8928.                 @parameter_name = '-SrcThreads',
  8929.                 @parameter_value = '3'
  8930.         if (@retcode = 1 or @@ERROR <> 0)
  8931.             goto FAILURE
  8932.  
  8933.         exec @retcode = dbo.sp_add_agent_parameter 
  8934.                 @profile_id = @real_profile_id,
  8935.                 @parameter_name = '-DestThreads',
  8936.                 @parameter_value = '4'
  8937.         if (@retcode = 1 or @@ERROR <> 0)
  8938.             goto FAILURE
  8939.  
  8940.         exec @retcode = dbo.sp_add_agent_parameter
  8941.                 @profile_id = @real_profile_id,
  8942.                 @parameter_name = '-KeepAliveMessageInterval',
  8943.                 @parameter_value = '300'
  8944.         if (@retcode = 1 or @@ERROR <> 0)
  8945.             goto FAILURE
  8946.  
  8947.         exec @retcode = dbo.sp_add_agent_parameter
  8948.                 @profile_id = @real_profile_id,
  8949.                 @parameter_name = '-MetadataRetentionCleanup',
  8950.                 @parameter_value = '1'
  8951.         if (@retcode = 1 or @@ERROR <> 0)
  8952.             goto FAILURE
  8953.  
  8954.         end
  8955.  
  8956.     /* 
  8957.     ** Distribution agent (Synchronization Manager Profile)
  8958.     */
  8959.     else if (@profile_id = 10)
  8960.         begin
  8961.         exec @retcode = dbo.sp_add_agent_parameter 
  8962.                 @profile_id = @real_profile_id,
  8963.                 @parameter_name = '-BcpBatchSize',
  8964.                 @parameter_value = '1000'
  8965.         if (@retcode = 1 or @@ERROR <> 0)
  8966.             goto FAILURE
  8967.  
  8968.         exec @retcode = dbo.sp_add_agent_parameter 
  8969.                 @profile_id = @real_profile_id,
  8970.                 @parameter_name = '-CommitBatchSize',
  8971.                 @parameter_value = '100'
  8972.         if (@retcode = 1 or @@ERROR <> 0)
  8973.             goto FAILURE
  8974.  
  8975.         exec @retcode = dbo.sp_add_agent_parameter 
  8976.                 @profile_id = @real_profile_id,
  8977.                 @parameter_name = '-CommitBatchThreshold',
  8978.                 @parameter_value = '1000'
  8979.         if (@retcode = 1 or @@ERROR <> 0)
  8980.             goto FAILURE
  8981.  
  8982.         exec @retcode = dbo.sp_add_agent_parameter 
  8983.                 @profile_id = @real_profile_id,
  8984.                 @parameter_name = '-HistoryVerboseLevel',
  8985.                 @parameter_value = '1'
  8986.         if (@retcode = 1 or @@ERROR <> 0)
  8987.             goto FAILURE
  8988.  
  8989.         exec @retcode = dbo.sp_add_agent_parameter 
  8990.                 @profile_id = @real_profile_id,
  8991.                 @parameter_name = '-LoginTimeout',
  8992.                 @parameter_value = '15'
  8993.         if (@retcode = 1 or @@ERROR <> 0)
  8994.             goto FAILURE
  8995.  
  8996.         exec @retcode = dbo.sp_add_agent_parameter 
  8997.                 @profile_id = @real_profile_id,
  8998.                 @parameter_name = '-MaxBcpThreads',
  8999.                 @parameter_value = '1'
  9000.         if (@retcode = 1 or @@ERROR <> 0)
  9001.             goto FAILURE
  9002.  
  9003.         exec @retcode = dbo.sp_add_agent_parameter 
  9004.                 @profile_id = @real_profile_id,
  9005.                 @parameter_name = '-MaxDeliveredTransactions',
  9006.                 @parameter_value = '0'
  9007.         if (@retcode = 1 or @@ERROR <> 0)
  9008.             goto FAILURE
  9009.  
  9010.         exec @retcode = dbo.sp_add_agent_parameter 
  9011.                 @profile_id = @real_profile_id,
  9012.                 @parameter_name = '-PollingInterval',
  9013.                 @parameter_value = '10'
  9014.         if (@retcode = 1 or @@ERROR <> 0)
  9015.             goto FAILURE
  9016.  
  9017.         exec @retcode = dbo.sp_add_agent_parameter 
  9018.                 @profile_id = @real_profile_id,
  9019.                 @parameter_name = '-QueryTimeout',
  9020.                 @parameter_value = '300'        -- 5 minutes 
  9021.         if (@retcode = 1 or @@ERROR <> 0)
  9022.             goto FAILURE
  9023.  
  9024.         exec @retcode = dbo.sp_add_agent_parameter 
  9025.                 @profile_id = @real_profile_id,
  9026.                 @parameter_name = '-TransactionsPerHistory',
  9027.                 @parameter_value = '100'
  9028.         if (@retcode = 1 or @@ERROR <> 0)
  9029.             goto FAILURE
  9030.  
  9031.         exec @retcode = dbo.sp_add_agent_parameter 
  9032.                 @profile_id = @real_profile_id,
  9033.                 @parameter_name = '-SkipErrors',
  9034.                 @parameter_value = ''
  9035.         if (@retcode = 1 or @@ERROR <> 0)
  9036.             goto FAILURE
  9037.  
  9038.         exec @retcode = dbo.sp_add_agent_parameter
  9039.                 @profile_id = @real_profile_id,
  9040.                 @parameter_name = '-KeepAliveMessageInterval',
  9041.                 @parameter_value = '300'
  9042.         if (@retcode = 1 or @@ERROR <> 0)
  9043.             goto FAILURE
  9044.  
  9045.         end
  9046.     
  9047.     /* 
  9048.     ** Queue Reader agent default profile
  9049.     */
  9050.     else if (@profile_id = 11)
  9051.     begin
  9052.         exec @retcode = dbo.sp_add_agent_parameter 
  9053.                 @profile_id = @real_profile_id,
  9054.                 @parameter_name = '-HistoryVerboseLevel',
  9055.                 @parameter_value = '1'
  9056.         if (@retcode = 1 or @@ERROR <> 0)
  9057.             goto FAILURE
  9058.  
  9059.         exec @retcode = dbo.sp_add_agent_parameter 
  9060.                 @profile_id = @real_profile_id,
  9061.                 @parameter_name = '-PollingInterval',
  9062.                 @parameter_value = '10'
  9063.         if (@retcode = 1 or @@ERROR <> 0)
  9064.             goto FAILURE
  9065.  
  9066.         exec @retcode = dbo.sp_add_agent_parameter 
  9067.                 @profile_id = @real_profile_id,
  9068.                 @parameter_name = '-LoginTimeout',
  9069.                 @parameter_value = '15'
  9070.         if (@retcode = 1 or @@ERROR <> 0)
  9071.             goto FAILURE
  9072.  
  9073.         exec @retcode = dbo.sp_add_agent_parameter 
  9074.                 @profile_id = @real_profile_id,
  9075.                 @parameter_name = '-QueryTimeout',
  9076.                 @parameter_value = '300'        -- 5 minutes 
  9077.         if (@retcode = 1 or @@ERROR <> 0)
  9078.             goto FAILURE
  9079.     end
  9080.       
  9081.     /* 
  9082.     ** Merge agent : Rowcount Validation profile for well connected scenarios 
  9083.     */
  9084.     else if (@profile_id = 12)
  9085.         begin
  9086.         exec @retcode = dbo.sp_add_agent_parameter 
  9087.                 @profile_id = @real_profile_id,
  9088.                 @parameter_name = '-PollingInterval',
  9089.                 @parameter_value = '60'
  9090.         if (@retcode = 1 or @@ERROR <> 0)
  9091.             goto FAILURE
  9092.  
  9093.  
  9094.         exec @retcode = dbo.sp_add_agent_parameter 
  9095.                 @profile_id = @real_profile_id,
  9096.                 @parameter_name = '-StartQueueTimeout',
  9097.                 @parameter_value = '0'
  9098.         if (@retcode = 1 or @@ERROR <> 0)
  9099.             goto FAILURE
  9100.  
  9101.         exec @retcode = dbo.sp_add_agent_parameter 
  9102.                 @profile_id = @real_profile_id,
  9103.                 @parameter_name = '-MaxDownloadChanges',
  9104.                 @parameter_value = '0'
  9105.         if (@retcode = 1 or @@ERROR <> 0)
  9106.             goto FAILURE
  9107.  
  9108.         exec @retcode = dbo.sp_add_agent_parameter 
  9109.                 @profile_id = @real_profile_id,
  9110.                 @parameter_name = '-MaxUploadChanges',
  9111.                 @parameter_value = '0'
  9112.         if (@retcode = 1 or @@ERROR <> 0)
  9113.             goto FAILURE
  9114.  
  9115.         exec @retcode = dbo.sp_add_agent_parameter 
  9116.                 @profile_id = @real_profile_id,
  9117.                 @parameter_name = '-ValidateInterval',
  9118.                 @parameter_value = '60'
  9119.         if (@retcode = 1 or @@ERROR <> 0)
  9120.             goto FAILURE
  9121.  
  9122.         exec @retcode = dbo.sp_add_agent_parameter 
  9123.                 @profile_id = @real_profile_id,
  9124.                 @parameter_name = '-LoginTimeout',
  9125.                 @parameter_value = '15'
  9126.         if (@retcode = 1 or @@ERROR <> 0)
  9127.             goto FAILURE
  9128.  
  9129.         exec @retcode = dbo.sp_add_agent_parameter 
  9130.                 @profile_id = @real_profile_id,
  9131.                 @parameter_name = '-QueryTimeout',
  9132.                 @parameter_value = '300'        
  9133.         if (@retcode = 1 or @@ERROR <> 0)
  9134.             goto FAILURE
  9135.  
  9136.         exec @retcode = dbo.sp_add_agent_parameter 
  9137.                 @profile_id = @real_profile_id,
  9138.                 @parameter_name = '-UploadGenerationsPerBatch',
  9139.                 @parameter_value = '100'
  9140.         if (@retcode = 1 or @@ERROR <> 0)
  9141.             goto FAILURE
  9142.  
  9143.         exec @retcode = dbo.sp_add_agent_parameter 
  9144.                 @profile_id = @real_profile_id,
  9145.                 @parameter_name = '-DownloadGenerationsPerBatch',
  9146.                 @parameter_value = '100'
  9147.         if (@retcode = 1 or @@ERROR <> 0)
  9148.             goto FAILURE
  9149.  
  9150.         exec @retcode = dbo.sp_add_agent_parameter 
  9151.                 @profile_id = @real_profile_id,
  9152.                 @parameter_name = '-UploadReadChangesPerBatch',
  9153.                 @parameter_value = '100'
  9154.         if (@retcode = 1 or @@ERROR <> 0)
  9155.             goto FAILURE
  9156.  
  9157.         exec @retcode = dbo.sp_add_agent_parameter 
  9158.                 @profile_id = @real_profile_id,
  9159.                 @parameter_name = '-DownloadReadChangesPerBatch',
  9160.                 @parameter_value = '100'
  9161.         if (@retcode = 1 or @@ERROR <> 0)
  9162.             goto FAILURE
  9163.  
  9164.         exec @retcode = dbo.sp_add_agent_parameter 
  9165.                 @profile_id = @real_profile_id,
  9166.                 @parameter_name = '-UploadWriteChangesPerBatch',
  9167.                 @parameter_value = '100'
  9168.         if (@retcode = 1 or @@ERROR <> 0)
  9169.             goto FAILURE
  9170.  
  9171.         exec @retcode = dbo.sp_add_agent_parameter 
  9172.                 @profile_id = @real_profile_id,
  9173.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  9174.                 @parameter_value = '100'
  9175.         if (@retcode = 1 or @@ERROR <> 0)
  9176.             goto FAILURE
  9177.  
  9178.         exec @retcode = dbo.sp_add_agent_parameter 
  9179.                 @profile_id = @real_profile_id,
  9180.                 @parameter_name = '-Validate',
  9181.                 @parameter_value = '1'
  9182.         if (@retcode = 1 or @@ERROR <> 0)
  9183.             goto FAILURE
  9184.  
  9185.         exec @retcode = dbo.sp_add_agent_parameter 
  9186.                 @profile_id = @real_profile_id,
  9187.                 @parameter_name = '-FastRowCount',
  9188.                 @parameter_value = '1'
  9189.         if (@retcode = 1 or @@ERROR <> 0)
  9190.             goto FAILURE
  9191.         
  9192.         exec @retcode = dbo.sp_add_agent_parameter 
  9193.                 @profile_id = @real_profile_id,
  9194.                 @parameter_name = '-HistoryVerboseLevel',
  9195.                 @parameter_value = '1'
  9196.         if (@retcode = 1 or @@ERROR <> 0)
  9197.             goto FAILURE
  9198.  
  9199.         exec @retcode = dbo.sp_add_agent_parameter 
  9200.                 @profile_id = @real_profile_id,
  9201.                 @parameter_name = '-ChangesPerHistory',
  9202.                 @parameter_value = '100'
  9203.         if (@retcode = 1 or @@ERROR <> 0)
  9204.             goto FAILURE
  9205.         exec @retcode = dbo.sp_add_agent_parameter 
  9206.                 @profile_id = @real_profile_id,
  9207.                 @parameter_name = '-BcpBatchSize',
  9208.                 @parameter_value = '100000'
  9209.         if (@retcode = 1 or @@ERROR <> 0)
  9210.             goto FAILURE
  9211.  
  9212.         exec @retcode = dbo.sp_add_agent_parameter 
  9213.                 @profile_id = @real_profile_id,
  9214.                 @parameter_name = '-NumDeadlockRetries',
  9215.                 @parameter_value = '5'
  9216.         if (@retcode = 1 or @@ERROR <> 0)
  9217.             goto FAILURE
  9218.  
  9219.         exec @retcode = dbo.sp_add_agent_parameter 
  9220.                 @profile_id = @real_profile_id,
  9221.                 @parameter_name = '-SrcThreads',
  9222.                 @parameter_value = '3'
  9223.         if (@retcode = 1 or @@ERROR <> 0)
  9224.             goto FAILURE
  9225.  
  9226.         exec @retcode = dbo.sp_add_agent_parameter 
  9227.                 @profile_id = @real_profile_id,
  9228.                 @parameter_name = '-DestThreads',
  9229.                 @parameter_value = '4'
  9230.         if (@retcode = 1 or @@ERROR <> 0)
  9231.             goto FAILURE
  9232.             
  9233.         exec @retcode = dbo.sp_add_agent_parameter
  9234.                 @profile_id = @real_profile_id,
  9235.                 @parameter_name = '-KeepAliveMessageInterval',
  9236.                 @parameter_value = '300'
  9237.         if (@retcode = 1 or @@ERROR <> 0)
  9238.             goto FAILURE
  9239.  
  9240.         exec @retcode = dbo.sp_add_agent_parameter
  9241.                 @profile_id = @real_profile_id,
  9242.                 @parameter_name = '-MetadataRetentionCleanup',
  9243.                 @parameter_value = '1'
  9244.         if (@retcode = 1 or @@ERROR <> 0)
  9245.             goto FAILURE
  9246.  
  9247.         end
  9248.  
  9249.     /* 
  9250.     ** Merge agent : Rowcount & Checksum Validation profile for well connected scenarios 
  9251.     */
  9252.     else if (@profile_id = 13)
  9253.         begin
  9254.         exec @retcode = dbo.sp_add_agent_parameter 
  9255.                 @profile_id = @real_profile_id,
  9256.                 @parameter_name = '-PollingInterval',
  9257.                 @parameter_value = '60'
  9258.         if (@retcode = 1 or @@ERROR <> 0)
  9259.             goto FAILURE
  9260.  
  9261.         exec @retcode = dbo.sp_add_agent_parameter 
  9262.                 @profile_id = @real_profile_id,
  9263.                 @parameter_name = '-StartQueueTimeout',
  9264.                 @parameter_value = '0'
  9265.         if (@retcode = 1 or @@ERROR <> 0)
  9266.             goto FAILURE
  9267.  
  9268.         exec @retcode = dbo.sp_add_agent_parameter 
  9269.                 @profile_id = @real_profile_id,
  9270.                 @parameter_name = '-MaxDownloadChanges',
  9271.                 @parameter_value = '0'
  9272.         if (@retcode = 1 or @@ERROR <> 0)
  9273.             goto FAILURE
  9274.  
  9275.         exec @retcode = dbo.sp_add_agent_parameter 
  9276.                 @profile_id = @real_profile_id,
  9277.                 @parameter_name = '-MaxUploadChanges',
  9278.                 @parameter_value = '0'
  9279.         if (@retcode = 1 or @@ERROR <> 0)
  9280.             goto FAILURE
  9281.  
  9282.         exec @retcode = dbo.sp_add_agent_parameter 
  9283.                 @profile_id = @real_profile_id,
  9284.                 @parameter_name = '-ValidateInterval',
  9285.                 @parameter_value = '60'
  9286.         if (@retcode = 1 or @@ERROR <> 0)
  9287.             goto FAILURE
  9288.  
  9289.         exec @retcode = dbo.sp_add_agent_parameter 
  9290.                 @profile_id = @real_profile_id,
  9291.                 @parameter_name = '-LoginTimeout',
  9292.                 @parameter_value = '15'
  9293.         if (@retcode = 1 or @@ERROR <> 0)
  9294.             goto FAILURE
  9295.  
  9296.         exec @retcode = dbo.sp_add_agent_parameter 
  9297.                 @profile_id = @real_profile_id,
  9298.                 @parameter_name = '-QueryTimeout',
  9299.                 @parameter_value = '300'        
  9300.         if (@retcode = 1 or @@ERROR <> 0)
  9301.             goto FAILURE
  9302.  
  9303.         exec @retcode = dbo.sp_add_agent_parameter 
  9304.                 @profile_id = @real_profile_id,
  9305.                 @parameter_name = '-UploadGenerationsPerBatch',
  9306.                 @parameter_value = '100'
  9307.         if (@retcode = 1 or @@ERROR <> 0)
  9308.             goto FAILURE
  9309.  
  9310.         exec @retcode = dbo.sp_add_agent_parameter 
  9311.                 @profile_id = @real_profile_id,
  9312.                 @parameter_name = '-DownloadGenerationsPerBatch',
  9313.                 @parameter_value = '100'
  9314.         if (@retcode = 1 or @@ERROR <> 0)
  9315.             goto FAILURE
  9316.  
  9317.         exec @retcode = dbo.sp_add_agent_parameter 
  9318.                 @profile_id = @real_profile_id,
  9319.                 @parameter_name = '-UploadReadChangesPerBatch',
  9320.                 @parameter_value = '100'
  9321.         if (@retcode = 1 or @@ERROR <> 0)
  9322.             goto FAILURE
  9323.  
  9324.         exec @retcode = dbo.sp_add_agent_parameter 
  9325.                 @profile_id = @real_profile_id,
  9326.                 @parameter_name = '-DownloadReadChangesPerBatch',
  9327.                 @parameter_value = '100'
  9328.         if (@retcode = 1 or @@ERROR <> 0)
  9329.             goto FAILURE
  9330.  
  9331.         exec @retcode = dbo.sp_add_agent_parameter 
  9332.                 @profile_id = @real_profile_id,
  9333.                 @parameter_name = '-UploadWriteChangesPerBatch',
  9334.                 @parameter_value = '100'
  9335.         if (@retcode = 1 or @@ERROR <> 0)
  9336.             goto FAILURE
  9337.  
  9338.         exec @retcode = dbo.sp_add_agent_parameter 
  9339.                 @profile_id = @real_profile_id,
  9340.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  9341.                 @parameter_value = '100'
  9342.         if (@retcode = 1 or @@ERROR <> 0)
  9343.             goto FAILURE
  9344.  
  9345.         exec @retcode = dbo.sp_add_agent_parameter 
  9346.                 @profile_id = @real_profile_id,
  9347.                 @parameter_name = '-Validate',
  9348.                 @parameter_value = '3'
  9349.         if (@retcode = 1 or @@ERROR <> 0)
  9350.             goto FAILURE
  9351.  
  9352.         exec @retcode = dbo.sp_add_agent_parameter 
  9353.                 @profile_id = @real_profile_id,
  9354.                 @parameter_name = '-FastRowCount',
  9355.                 @parameter_value = '1'
  9356.         if (@retcode = 1 or @@ERROR <> 0)
  9357.             goto FAILURE
  9358.         
  9359.         exec @retcode = dbo.sp_add_agent_parameter 
  9360.                 @profile_id = @real_profile_id,
  9361.                 @parameter_name = '-HistoryVerboseLevel',
  9362.                 @parameter_value = '1'
  9363.         if (@retcode = 1 or @@ERROR <> 0)
  9364.             goto FAILURE
  9365.  
  9366.         exec @retcode = dbo.sp_add_agent_parameter 
  9367.                 @profile_id = @real_profile_id,
  9368.                 @parameter_name = '-ChangesPerHistory',
  9369.                 @parameter_value = '100'
  9370.         if (@retcode = 1 or @@ERROR <> 0)
  9371.             goto FAILURE
  9372.         exec @retcode = dbo.sp_add_agent_parameter 
  9373.                 @profile_id = @real_profile_id,
  9374.                 @parameter_name = '-BcpBatchSize',
  9375.                 @parameter_value = '100000'
  9376.         if (@retcode = 1 or @@ERROR <> 0)
  9377.             goto FAILURE
  9378.  
  9379.         exec @retcode = dbo.sp_add_agent_parameter 
  9380.                 @profile_id = @real_profile_id,
  9381.                 @parameter_name = '-NumDeadlockRetries',
  9382.                 @parameter_value = '5'
  9383.         if (@retcode = 1 or @@ERROR <> 0)
  9384.             goto FAILURE
  9385.             
  9386.         exec @retcode = dbo.sp_add_agent_parameter 
  9387.                 @profile_id = @real_profile_id,
  9388.                 @parameter_name = '-SrcThreads',
  9389.                 @parameter_value = '3'
  9390.         if (@retcode = 1 or @@ERROR <> 0)
  9391.             goto FAILURE
  9392.  
  9393.         exec @retcode = dbo.sp_add_agent_parameter 
  9394.                 @profile_id = @real_profile_id,
  9395.                 @parameter_name = '-DestThreads',
  9396.                 @parameter_value = '4'
  9397.         if (@retcode = 1 or @@ERROR <> 0)
  9398.             goto FAILURE
  9399.  
  9400.         exec @retcode = dbo.sp_add_agent_parameter
  9401.                 @profile_id = @real_profile_id,
  9402.                 @parameter_name = '-KeepAliveMessageInterval',
  9403.                 @parameter_value = '300'
  9404.         if (@retcode = 1 or @@ERROR <> 0)
  9405.             goto FAILURE
  9406.  
  9407.         exec @retcode = dbo.sp_add_agent_parameter
  9408.                 @profile_id = @real_profile_id,
  9409.                 @parameter_name = '-MetadataRetentionCleanup',
  9410.                 @parameter_value = '1'
  9411.         if (@retcode = 1 or @@ERROR <> 0)
  9412.             goto FAILURE
  9413.  
  9414.         end
  9415.  
  9416.     /* 
  9417.     ** Distribution agent, skip error profile
  9418.     */
  9419.     else if (@profile_id = 14)
  9420.       begin
  9421.  
  9422.         exec @retcode = dbo.sp_add_agent_parameter 
  9423.                 @profile_id = @real_profile_id,
  9424.                 @parameter_name = '-BcpBatchSize',
  9425.                 @parameter_value = '100000'
  9426.         if (@retcode = 1 or @@ERROR <> 0)
  9427.             goto FAILURE
  9428.  
  9429.         exec @retcode = dbo.sp_add_agent_parameter 
  9430.                 @profile_id = @real_profile_id,
  9431.                 @parameter_name = '-CommitBatchSize',
  9432.                 @parameter_value = '100'
  9433.         if (@retcode = 1 or @@ERROR <> 0)
  9434.             goto FAILURE
  9435.  
  9436.         exec @retcode = dbo.sp_add_agent_parameter 
  9437.                 @profile_id = @real_profile_id,
  9438.                 @parameter_name = '-CommitBatchThreshold',
  9439.                 @parameter_value = '1000'
  9440.         if (@retcode = 1 or @@ERROR <> 0)
  9441.             goto FAILURE
  9442.  
  9443.         exec @retcode = dbo.sp_add_agent_parameter 
  9444.                 @profile_id = @real_profile_id,
  9445.                 @parameter_name = '-HistoryVerboseLevel',
  9446.                 @parameter_value = '1'
  9447.         if (@retcode = 1 or @@ERROR <> 0)
  9448.             goto FAILURE
  9449.  
  9450.         exec @retcode = dbo.sp_add_agent_parameter 
  9451.                 @profile_id = @real_profile_id,
  9452.                 @parameter_name = '-LoginTimeout',
  9453.                 @parameter_value = '15'
  9454.         if (@retcode = 1 or @@ERROR <> 0)
  9455.             goto FAILURE
  9456.  
  9457.         exec @retcode = dbo.sp_add_agent_parameter 
  9458.                 @profile_id = @real_profile_id,
  9459.                 @parameter_name = '-MaxBcpThreads',
  9460.                 @parameter_value = '1'
  9461.         if (@retcode = 1 or @@ERROR <> 0)
  9462.             goto FAILURE
  9463.  
  9464.         exec @retcode = dbo.sp_add_agent_parameter 
  9465.                 @profile_id = @real_profile_id,
  9466.                 @parameter_name = '-MaxDeliveredTransactions',
  9467.                 @parameter_value = '0'
  9468.         if (@retcode = 1 or @@ERROR <> 0)
  9469.             goto FAILURE
  9470.  
  9471.         exec @retcode = dbo.sp_add_agent_parameter 
  9472.                 @profile_id = @real_profile_id,
  9473.                 @parameter_name = '-PollingInterval',
  9474.                 @parameter_value = '10'
  9475.         if (@retcode = 1 or @@ERROR <> 0)
  9476.             goto FAILURE
  9477.  
  9478.         exec @retcode = dbo.sp_add_agent_parameter 
  9479.                 @profile_id = @real_profile_id,
  9480.                 @parameter_name = '-QueryTimeout',
  9481.                 @parameter_value = '300'        -- 5 minutes 
  9482.         if (@retcode = 1 or @@ERROR <> 0)
  9483.             goto FAILURE
  9484.  
  9485.         exec @retcode = dbo.sp_add_agent_parameter 
  9486.                 @profile_id = @real_profile_id,
  9487.                 @parameter_name = '-TransactionsPerHistory',
  9488.                 @parameter_value = '100'
  9489.         if (@retcode = 1 or @@ERROR <> 0)
  9490.             goto FAILURE
  9491.  
  9492.         exec @retcode = dbo.sp_add_agent_parameter 
  9493.                 @profile_id = @real_profile_id,
  9494.                 @parameter_name = '-SkipErrors',
  9495.                 @parameter_value = '2601:2627:20598'
  9496.         if (@retcode = 1 or @@ERROR <> 0)
  9497.             goto FAILURE
  9498.  
  9499.         exec @retcode = dbo.sp_add_agent_parameter
  9500.                 @profile_id = @real_profile_id,
  9501.                 @parameter_name = '-KeepAliveMessageInterval',
  9502.                 @parameter_value = '300'
  9503.         if (@retcode = 1 or @@ERROR <> 0)
  9504.             goto FAILURE
  9505.  
  9506.         end
  9507.  
  9508.     /* 
  9509.     ** Merge agent : Rowcount Validation profile for high volumn server-to-server
  9510.     */
  9511.         else if (@profile_id = 15)
  9512.         begin
  9513.         exec @retcode = dbo.sp_add_agent_parameter 
  9514.                 @profile_id = @real_profile_id,
  9515.                 @parameter_name = '-PollingInterval',
  9516.                 @parameter_value = '60'
  9517.         if (@retcode = 1 or @@ERROR <> 0)
  9518.             goto FAILURE
  9519.  
  9520.         exec @retcode = dbo.sp_add_agent_parameter 
  9521.                 @profile_id = @real_profile_id,
  9522.                 @parameter_name = '-StartQueueTimeout',
  9523.                 @parameter_value = '0'
  9524.         if (@retcode = 1 or @@ERROR <> 0)
  9525.             goto FAILURE
  9526.  
  9527.         exec @retcode = dbo.sp_add_agent_parameter 
  9528.                 @profile_id = @real_profile_id,
  9529.                 @parameter_name = '-MaxDownloadChanges',
  9530.                 @parameter_value = '0'
  9531.         if (@retcode = 1 or @@ERROR <> 0)
  9532.             goto FAILURE
  9533.  
  9534.         exec @retcode = dbo.sp_add_agent_parameter 
  9535.                 @profile_id = @real_profile_id,
  9536.                 @parameter_name = '-MaxUploadChanges',
  9537.                 @parameter_value = '0'
  9538.         if (@retcode = 1 or @@ERROR <> 0)
  9539.             goto FAILURE
  9540.  
  9541.         exec @retcode = dbo.sp_add_agent_parameter 
  9542.                 @profile_id = @real_profile_id,
  9543.                 @parameter_name = '-ValidateInterval',
  9544.                 @parameter_value = '60'
  9545.         if (@retcode = 1 or @@ERROR <> 0)
  9546.             goto FAILURE
  9547.  
  9548.         exec @retcode = dbo.sp_add_agent_parameter 
  9549.                 @profile_id = @real_profile_id,
  9550.                 @parameter_name = '-LoginTimeout',
  9551.                 @parameter_value = '15'
  9552.         if (@retcode = 1 or @@ERROR <> 0)
  9553.             goto FAILURE
  9554.  
  9555.         exec @retcode = dbo.sp_add_agent_parameter 
  9556.                 @profile_id = @real_profile_id,
  9557.                 @parameter_name = '-QueryTimeout',
  9558.                 @parameter_value = '600'        
  9559.         if (@retcode = 1 or @@ERROR <> 0)
  9560.             goto FAILURE
  9561.  
  9562.         exec @retcode = dbo.sp_add_agent_parameter 
  9563.                 @profile_id = @real_profile_id,
  9564.                 @parameter_name = '-UploadGenerationsPerBatch',
  9565.                 @parameter_value = '2000'
  9566.         if (@retcode = 1 or @@ERROR <> 0)
  9567.             goto FAILURE
  9568.  
  9569.         exec @retcode = dbo.sp_add_agent_parameter 
  9570.                 @profile_id = @real_profile_id,
  9571.                 @parameter_name = '-DownloadGenerationsPerBatch',
  9572.                 @parameter_value = '2000'
  9573.         if (@retcode = 1 or @@ERROR <> 0)
  9574.             goto FAILURE
  9575.  
  9576.         exec @retcode = dbo.sp_add_agent_parameter 
  9577.                 @profile_id = @real_profile_id,
  9578.                 @parameter_name = '-UploadReadChangesPerBatch',
  9579.                 @parameter_value = '100'
  9580.         if (@retcode = 1 or @@ERROR <> 0)
  9581.             goto FAILURE
  9582.  
  9583.         exec @retcode = dbo.sp_add_agent_parameter 
  9584.                 @profile_id = @real_profile_id,
  9585.                 @parameter_name = '-DownloadReadChangesPerBatch',
  9586.                 @parameter_value = '100'
  9587.         if (@retcode = 1 or @@ERROR <> 0)
  9588.             goto FAILURE
  9589.  
  9590.         exec @retcode = dbo.sp_add_agent_parameter 
  9591.                 @profile_id = @real_profile_id,
  9592.                 @parameter_name = '-UploadWriteChangesPerBatch',
  9593.                 @parameter_value = '100'
  9594.         if (@retcode = 1 or @@ERROR <> 0)
  9595.             goto FAILURE
  9596.  
  9597.         exec @retcode = dbo.sp_add_agent_parameter 
  9598.                 @profile_id = @real_profile_id,
  9599.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  9600.                 @parameter_value = '100'
  9601.         if (@retcode = 1 or @@ERROR <> 0)
  9602.             goto FAILURE
  9603.  
  9604.         exec @retcode = dbo.sp_add_agent_parameter 
  9605.                 @profile_id = @real_profile_id,
  9606.                 @parameter_name = '-Validate',
  9607.                 @parameter_value = '0'
  9608.         if (@retcode = 1 or @@ERROR <> 0)
  9609.             goto FAILURE
  9610.  
  9611.         exec @retcode = dbo.sp_add_agent_parameter 
  9612.                 @profile_id = @real_profile_id,
  9613.                 @parameter_name = '-FastRowCount',
  9614.                 @parameter_value = '1'
  9615.         if (@retcode = 1 or @@ERROR <> 0)
  9616.             goto FAILURE
  9617.         
  9618.         exec @retcode = dbo.sp_add_agent_parameter 
  9619.                 @profile_id = @real_profile_id,
  9620.                 @parameter_name = '-HistoryVerboseLevel',
  9621.                 @parameter_value = '1'
  9622.         if (@retcode = 1 or @@ERROR <> 0)
  9623.             goto FAILURE
  9624.  
  9625.         exec @retcode = dbo.sp_add_agent_parameter 
  9626.                 @profile_id = @real_profile_id,
  9627.                 @parameter_name = '-ChangesPerHistory',
  9628.                 @parameter_value = '100'
  9629.         if (@retcode = 1 or @@ERROR <> 0)
  9630.             goto FAILURE
  9631.  
  9632.         exec @retcode = dbo.sp_add_agent_parameter 
  9633.                 @profile_id = @real_profile_id,
  9634.                 @parameter_name = '-BcpBatchSize',
  9635.                 @parameter_value = '100000'
  9636.         if (@retcode = 1 or @@ERROR <> 0)
  9637.             goto FAILURE
  9638.  
  9639.         exec @retcode = dbo.sp_add_agent_parameter 
  9640.                 @profile_id = @real_profile_id,
  9641.                 @parameter_name = '-NumDeadlockRetries',
  9642.                 @parameter_value = '5'
  9643.         if (@retcode = 1 or @@ERROR <> 0)
  9644.             goto FAILURE
  9645.  
  9646.         exec @retcode = dbo.sp_add_agent_parameter 
  9647.                 @profile_id = @real_profile_id,
  9648.                 @parameter_name = '-SrcThreads',
  9649.                 @parameter_value = '3'
  9650.         if (@retcode = 1 or @@ERROR <> 0)
  9651.             goto FAILURE
  9652.  
  9653.         exec @retcode = dbo.sp_add_agent_parameter 
  9654.                 @profile_id = @real_profile_id,
  9655.                 @parameter_name = '-DestThreads',
  9656.                 @parameter_value = '4'
  9657.         if (@retcode = 1 or @@ERROR <> 0)
  9658.             goto FAILURE
  9659.  
  9660.         exec @retcode = dbo.sp_add_agent_parameter
  9661.                 @profile_id = @real_profile_id,
  9662.                 @parameter_name = '-KeepAliveMessageInterval',
  9663.                 @parameter_value = '300'
  9664.         if (@retcode = 1 or @@ERROR <> 0)
  9665.             goto FAILURE
  9666.  
  9667.         exec @retcode = dbo.sp_add_agent_parameter
  9668.                 @profile_id = @real_profile_id,
  9669.                 @parameter_name = '-MetadataRetentionCleanup',
  9670.                 @parameter_value = '1'
  9671.         if (@retcode = 1 or @@ERROR <> 0)
  9672.             goto FAILURE
  9673.  
  9674.         end
  9675.  
  9676.     return 0
  9677. FAILURE:
  9678.     return 1
  9679. go
  9680.  
  9681.  
  9682. raiserror('Creating procedure sp_MSvalidate_agent_parameter', 0,1)
  9683. go
  9684. create procedure sp_MSvalidate_agent_parameter (
  9685.     @profile_id      int,
  9686.     @parameter_name  sysname,
  9687.     @parameter_value nvarchar(255)
  9688. )
  9689. as
  9690.     declare @agent_type  int
  9691.     declare @original_parameter_name sysname
  9692.     declare @numeric_value int
  9693.  
  9694.     -- Make sure parameters are non-null
  9695.     if @profile_id is null
  9696.     BEGIN
  9697.         RAISERROR (14043, 16, -1, '@profile_id')
  9698.         RETURN (1)
  9699.     END
  9700.  
  9701.     if @parameter_name is null 
  9702.     BEGIN
  9703.         RAISERROR (14043, 16, -1, '@parameter_name')
  9704.         RETURN (1)
  9705.     END
  9706.  
  9707.     IF @parameter_value is null
  9708.     BEGIN
  9709.         RAISERROR (14043, 16, -1, '@parameter_value')
  9710.         RETURN (1)
  9711.     END
  9712.  
  9713.     select @original_parameter_name = @parameter_name
  9714.     
  9715.     select @agent_type = agent_type
  9716.     from msdb..MSagent_profiles 
  9717.     where profile_id = @profile_id
  9718.  
  9719.     -- Parameter name validation
  9720.     if (substring(@parameter_name, 1, 1) <> '/' and 
  9721.         substring(@parameter_name, 1, 1) <> '-')
  9722.     begin
  9723.         return 1
  9724.     end
  9725.  
  9726.     select @parameter_name = lower(substring(@parameter_name, 2, len(@parameter_name) - 1) collate SQL_Latin1_General_CP1_CS_AS)
  9727.  
  9728.     -- Snapshot agent - agent_type = 1
  9729.     if (@agent_type = 1)
  9730.     begin
  9731.         if not @parameter_name in ( 
  9732.             N'bcpbatchsize',   
  9733.             N'historyverboselevel',
  9734.             N'logintimeout',
  9735.             N'maxbcpthreads',
  9736.             N'querytimeout',
  9737.             N'startqueuetimeout',
  9738.             N'maxnetworkoptimization',
  9739.             N'useperarticlecontentsview'
  9740.             )    
  9741.         begin
  9742.             raiserror(21111, 16, -1, @original_parameter_name)
  9743.             return 1
  9744.         end
  9745.     end
  9746.     -- Logreader - agent_type = 2
  9747.     else if (@agent_type =2)  
  9748.     begin
  9749.         if not lower(@parameter_name collate SQL_Latin1_General_CP1_CS_AS) in ( 
  9750.             N'historyverboselevel',
  9751.             N'logintimeout',
  9752.             N'pollinginterval',
  9753.             N'querytimeout',
  9754.             N'readbatchsize'
  9755.             )    
  9756.         begin
  9757.             raiserror(21112, 16, -1, @original_parameter_name)
  9758.             return 1
  9759.         end
  9760.     end
  9761.     -- Distribution agent - agent_type = 3
  9762.     else if (@agent_type = 3)
  9763.     begin
  9764.         if not @parameter_name in ( 
  9765.             N'bcpbatchsize',   
  9766.             N'commitbatchsize',   
  9767.             N'commitbatchthreshold',   
  9768.             N'historyverboselevel',
  9769.             N'logintimeout',
  9770.             N'maxbcpthreads',
  9771.             N'maxdeliveredtransactions',
  9772.             N'pollinginterval',
  9773.             N'querytimeout',
  9774.             N'transactionsperhistory',
  9775.             N'skiperrors',
  9776.             N'keepalivemessageinterval',
  9777.             N'useinprocloader'            
  9778.             )    
  9779.         begin
  9780.             raiserror(21113, 16, -1, @original_parameter_name)
  9781.             return 1
  9782.         end
  9783.     end
  9784.     -- Merge agent - agent_type = 4
  9785.     else if (@agent_type = 4)
  9786.     begin 
  9787.         if not @parameter_name in ( 
  9788.             N'startqueuetimeout',
  9789.             N'pollinginterval',   
  9790.             N'validateinterval',   
  9791.             N'logintimeout',   
  9792.             N'querytimeout',
  9793.             N'maxuploadchanges',
  9794.             N'maxdownloadchanges',
  9795.             N'uploadgenerationsperbatch',   
  9796.             N'downloadgenerationsperbatch',   
  9797.             N'uploadreadchangesperbatch',   
  9798.             N'downloadreadchangesperbatch',   
  9799.             N'uploadwritechangesperbatch',
  9800.             N'downloadwritechangesperbatch',
  9801.             N'validate',   
  9802.             N'fastrowcount',   
  9803.             N'historyverboselevel',
  9804.             N'changesperhistory',
  9805.             N'bcpbatchsize',
  9806.             N'numdeadlockretries',
  9807.             N'keepalivemessageinterval',
  9808.             N'srcthreads',
  9809.             N'destthreads',
  9810.             N'useinprocloader',
  9811.             N'metadataretentioncleanup'
  9812.             )    
  9813.         begin
  9814.             raiserror(21114, 16, -1, @original_parameter_name)
  9815.             return 1
  9816.         end
  9817.     end
  9818.     -- Qreader agent - agent_type = 9
  9819.     else if (@agent_type = 9)  
  9820.     begin
  9821.         if not lower(@parameter_name collate SQL_Latin1_General_CP1_CS_AS) in ( 
  9822.             N'resolverstate',
  9823.             N'sqlqueuemode',
  9824.             N'historyverboselevel',
  9825.             N'pollinginterval',
  9826.             N'logintimeout',
  9827.             N'querytimeout'
  9828.             )    
  9829.         begin
  9830.             raiserror(21112, 16, -1, @original_parameter_name)
  9831.             return 1
  9832.         end
  9833.     end
  9834.     else if @agent_type is null
  9835.     begin
  9836.         raiserror (20066, 16, -1)   -- profile not defined
  9837.         return 1
  9838.     end
  9839.     else
  9840.     begin
  9841.         -- MSagent_parameters table corruption
  9842.         return 1
  9843.     end
  9844.  
  9845.     -- Parameter value validation
  9846.     if (@parameter_name = N'bcpbatchsize')
  9847.     begin
  9848.         select @numeric_value = convert(int, @parameter_value)
  9849.         if @@error <> 0 or @numeric_value < 1
  9850.         begin
  9851.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9852.             return 1     
  9853.         end
  9854.     end
  9855.     else if (@parameter_name = N'commitbatchsize')
  9856.     begin
  9857.         select @numeric_value = convert(int, @parameter_value)
  9858.         if @@error <> 0 or @numeric_value < 1
  9859.         begin
  9860.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9861.             return 1     
  9862.         end                        
  9863.     end
  9864.     else if (@parameter_name = N'commitbatchthreshold')
  9865.     begin
  9866.         select @numeric_value = convert(int, @parameter_value)
  9867.         if @@error <> 0 or @numeric_value < 1
  9868.         begin
  9869.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9870.             return 1     
  9871.         end                        
  9872.     end
  9873.     else if (@parameter_name = N'downloadgenerationsperbatch')
  9874.     begin
  9875.         select @numeric_value = convert(int, @parameter_value)
  9876.         if @@error <> 0 or @numeric_value < 1
  9877.         begin
  9878.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9879.             return 1     
  9880.         end 
  9881.         if @numeric_value > 2000
  9882.         begin
  9883.             raiserror(14266, 16, -1, 'DownloadGenerationsPerBatch', '1 - 2000') 
  9884.             return 1
  9885.         end                      
  9886.     end
  9887.     else if (@parameter_name = N'downloadreadchangesperbatch')
  9888.     begin
  9889.         select @numeric_value = convert(int, @parameter_value)
  9890.         if @@error <> 0 or @numeric_value < 1
  9891.         begin
  9892.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9893.             return 1     
  9894.         end    
  9895.         if @numeric_value > 2000
  9896.         begin
  9897.             raiserror(14266, 16, -1, 'DownloadReadChangesPerBatch', '1 - 2000') 
  9898.             return 1
  9899.         end                      
  9900.     end
  9901.     else if (@parameter_name = N'downloadwritechangesperbatch')
  9902.     begin
  9903.         select @numeric_value = convert(int, @parameter_value)
  9904.         if @@error <> 0 or @numeric_value < 1
  9905.         begin
  9906.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9907.             return 1     
  9908.         end 
  9909.         if @numeric_value > 2000
  9910.         begin
  9911.             raiserror(14266, 16, -1, 'DownloadWriteChangesPerBatch', '1 - 2000') 
  9912.             return 1
  9913.         end                         
  9914.     end
  9915.     else if (@parameter_name = N'fastrowcount')
  9916.     begin
  9917.         select @numeric_value = convert(int, @parameter_value)
  9918.         if @@error <> 0 or not (@numeric_value in (1,2,3))
  9919.         begin
  9920.             raiserror(21116, 16, -1, @parameter_value, @original_parameter_name)
  9921.             return 1     
  9922.         end                        
  9923.     end
  9924.     else if (@parameter_name = N'historyverboselevel')
  9925.     begin
  9926.         select @numeric_value = convert(int, @parameter_value)
  9927.         if @@error <> 0 or not (@numeric_value in (0,1,2,3))
  9928.         begin
  9929.             raiserror(21117, 16, -1, @parameter_value, @original_parameter_name)
  9930.             return 1     
  9931.         end                        
  9932.     end
  9933.     else if (@parameter_name = N'logintimeout')
  9934.     begin
  9935.         select @numeric_value = convert(int, @parameter_value)
  9936.         if @@error <> 0 or @numeric_value < 1
  9937.         begin
  9938.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9939.             return 1     
  9940.         end                        
  9941.     end
  9942.     else if (@parameter_name = N'maxbcpthreads')
  9943.     begin
  9944.         select @numeric_value = convert(int, @parameter_value)
  9945.         if @@error <> 0 or @numeric_value < 1
  9946.         begin
  9947.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9948.             return 1     
  9949.         end                        
  9950.     end
  9951.     else if (@parameter_name = N'maxdeliveredtransactions')
  9952.     begin
  9953.         select @numeric_value = convert(int, @parameter_value)
  9954.         if @@error <> 0 or @numeric_value < 0
  9955.         begin
  9956.             raiserror(21119, 16, -1, @parameter_value, @original_parameter_name)
  9957.             return 1     
  9958.         end                        
  9959.     end
  9960.     else if (@parameter_name = N'pollinginterval')
  9961.     begin
  9962.         select @numeric_value = convert(int, @parameter_value)
  9963.         if @@error <> 0 or @numeric_value < 1
  9964.         begin
  9965.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9966.             return 1     
  9967.         end                        
  9968.     end
  9969.     else if (@parameter_name = N'querytimeout')
  9970.     begin
  9971.         select @numeric_value = convert(int, @parameter_value)
  9972.         if @@error <> 0 or @numeric_value < 1
  9973.         begin
  9974.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9975.             return 1     
  9976.         end                        
  9977.     end
  9978.     else if (@parameter_name = N'readbatchsize')
  9979.     begin
  9980.         select @numeric_value = convert(int, @parameter_value)
  9981.         if @@error <> 0 or @numeric_value < 1
  9982.         begin
  9983.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9984.             return 1     
  9985.         end                        
  9986.     end
  9987.     else if (@parameter_name = N'transactionsperhistory')
  9988.     begin
  9989.         select @numeric_value = convert(int, @parameter_value)
  9990.         if @@error <> 0 or @numeric_value not between 0 and 10000
  9991.         begin
  9992.             raiserror(211118, 16, -1, @parameter_value, @original_parameter_name)
  9993.             return 1     
  9994.         end                        
  9995.     end
  9996.     else if (@parameter_name = N'uploadgenerationsperbatch')
  9997.     begin
  9998.         select @numeric_value = convert(int, @parameter_value)
  9999.         if @@error <> 0 or @numeric_value < 1
  10000.         begin
  10001.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10002.             return 1     
  10003.         end 
  10004.         if @numeric_value > 2000
  10005.         begin
  10006.             raiserror(14266, 16, -1, 'UploadGenerationsPerBatch', '1 - 2000') 
  10007.             return 1
  10008.         end                         
  10009.     end
  10010.     else if (@parameter_name = N'uploadreadchangesperbatch')
  10011.     begin
  10012.         select @numeric_value = convert(int, @parameter_value)
  10013.         if @@error <> 0 or @numeric_value < 1
  10014.         begin
  10015.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10016.             return 1     
  10017.         end     
  10018.         if @numeric_value > 2000
  10019.         begin
  10020.             raiserror(14266, 16, -1, 'UploadReadChangesPerBatch', '1 - 2000') 
  10021.             return 1
  10022.         end                     
  10023.     end
  10024.     else if (@parameter_name = N'uploadwritechangesperbatch')
  10025.     begin
  10026.         select @numeric_value = convert(int, @parameter_value)
  10027.         if @@error <> 0 or @numeric_value < 1
  10028.         begin
  10029.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10030.             return 1     
  10031.         end 
  10032.         if @numeric_value > 2000
  10033.         begin
  10034.             raiserror(14266, 16, -1, 'UploadWriteChangesPerBatch', '1 - 2000') 
  10035.             return 1
  10036.         end                         
  10037.     end
  10038.     else if (@parameter_name = N'validate')
  10039.     begin
  10040.         select @numeric_value = convert(int, @parameter_value)
  10041.         if @@error <> 0 or not (@numeric_value in (0,1,2,3))
  10042.         begin
  10043.             raiserror(21117, 16, -1, @parameter_value, @original_parameter_name)
  10044.             return 1     
  10045.         end                        
  10046.     end
  10047.     else if (@parameter_name = N'validateinterval')
  10048.     begin
  10049.         select @numeric_value = convert(int, @parameter_value)
  10050.         if @@error <> 0 or @numeric_value < 1
  10051.         begin
  10052.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10053.             return 1     
  10054.         end                        
  10055.     end
  10056.     else if (@parameter_name = N'skiperrors')
  10057.     begin
  10058.         -- Empty string is valid.
  10059.         if @parameter_value <> N''
  10060.         begin
  10061.             -- Valid format: 11:22:33
  10062.             if    @parameter_value like '%[^0-9:]%' or
  10063.                 @parameter_value like ':%' or
  10064.                 @parameter_value like '%:' or
  10065.                 @parameter_value like '%::%'
  10066.             begin
  10067.                 raiserror(20601, 16, -1)
  10068.                 return 1
  10069.             end
  10070.             -- cannot has number of errors equals to or more than 11
  10071.             if    @parameter_value like '%:%:%:%:%:%:%:%:%:%:%'
  10072.             begin
  10073.                 raiserror(20602, 16, -1)
  10074.                 return 1
  10075.             end
  10076.         end
  10077.     end
  10078.     -- Parameter value validation
  10079.     else if (@parameter_name = N'numdeadlockretries')
  10080.     begin
  10081.         select @numeric_value = convert(int, @parameter_value)
  10082.         if @@error <> 0 or @numeric_value < 1
  10083.         begin
  10084.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10085.             return 1     
  10086.         end
  10087.         if @numeric_value > 100
  10088.         begin
  10089.             raiserror(14266, 16, -1, 'NumDeadlockRetries', '1 - 100') 
  10090.             return 1
  10091.         end                         
  10092.     end
  10093.     else if (@parameter_name = N'srcthreads')
  10094.     begin
  10095.         select @numeric_value = convert(int, @parameter_value)
  10096.         if @@error <> 0 or @numeric_value < 1
  10097.         begin
  10098.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10099.             return 1     
  10100.         end                        
  10101.     end
  10102.     else if (@parameter_name = N'destthreads')
  10103.     begin
  10104.         select @numeric_value = convert(int, @parameter_value)
  10105.         if @@error <> 0 or @numeric_value < 1
  10106.         begin
  10107.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10108.             return 1     
  10109.         end                        
  10110.     end
  10111.     else if (@parameter_name = N'keepalivemessageinterval')
  10112.     begin
  10113.         select @numeric_value = convert(int, @parameter_value)
  10114.         if @@error <> 0 or @numeric_value < 30
  10115.         begin
  10116.             raiserror(21405, 16, -1, @parameter_value, @original_parameter_name, 30)            
  10117.             return 1
  10118.         end
  10119.     end
  10120.     else if (@parameter_name = N'useinprocloader')
  10121.     begin
  10122.         select @numeric_value = convert(int, @parameter_value)
  10123.         if @@error <> 0 or @numeric_value not in (0, 1) or rtrim(@parameter_value) = N''
  10124.         begin
  10125.             raiserror(21406, 16, -1, @parameter_value, @original_parameter_name)
  10126.             return 1
  10127.         end
  10128.     end
  10129.     else if (@parameter_name = N'startqueuetimeout')
  10130.     begin
  10131.         select @numeric_value = convert(int, @parameter_value)
  10132.         if @@error <> 0 or (@numeric_value < 300 and @numeric_value <> 0) or rtrim(@parameter_value) = N''
  10133.         begin
  10134.             raiserror(21404, 16, -1, @parameter_value, @original_parameter_name)
  10135.             return 1
  10136.         end
  10137.     end
  10138.     else if (@parameter_name = N'resolverstate')
  10139.     begin
  10140.         select @numeric_value = convert(int, @parameter_value)
  10141.         if @@error <> 0 or @numeric_value not in (1,2,3)
  10142.         begin
  10143.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10144.             return 1     
  10145.         end                        
  10146.     end
  10147.     else if (@parameter_name = N'sqlqueuemode')
  10148.     begin
  10149.         select @numeric_value = convert(int, @parameter_value)
  10150.         if @@error <> 0 or @numeric_value not in (0,1,2)
  10151.         begin
  10152.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10153.             return 1     
  10154.         end                        
  10155.     end
  10156.     else if (@parameter_name = N'maxnetworkoptimization')
  10157.     begin
  10158.         select @numeric_value = convert(int, @parameter_value)
  10159.         if @@error <> 0 or @numeric_value not in (0, 1) or rtrim(@parameter_value) = N''
  10160.         begin
  10161.             raiserror(21406, 16, -1, @parameter_value, @original_parameter_name)
  10162.             return 1
  10163.         end
  10164.     end
  10165.     else if (@parameter_name = N'useperarticlecontentsview')
  10166.     begin
  10167.         select @numeric_value = convert(int, @parameter_value)
  10168.         if @@error <> 0 or @numeric_value not in (0, 1) or rtrim(@parameter_value) = N''
  10169.         begin
  10170.             raiserror(21406, 16, -1, @parameter_value, @original_parameter_name)
  10171.             return 1
  10172.         end
  10173.     end
  10174.     
  10175.     return 0
  10176. go 
  10177.  
  10178. EXEC dbo.sp_MS_marksystemobject 'sp_MSvalidate_agent_parameter'
  10179.  
  10180.  
  10181. raiserror('Creating procedure sp_add_agent_parameter', 0,1)
  10182. go
  10183.  
  10184. -- Add a row into the "MSagent_parameters" table
  10185. create procedure sp_add_agent_parameter (
  10186.     @profile_id int,
  10187.     @parameter_name         sysname,
  10188.     @parameter_value        nvarchar(255)
  10189. )
  10190. as
  10191.     declare @slash_parameter_name sysname
  10192.     declare @dash_parameter_name sysname
  10193.     declare @retcode int
  10194.     
  10195.     set nocount on
  10196.  
  10197.     select @parameter_name = RTRIM(@parameter_name)
  10198.  
  10199.     -- Append leading '-' when not given
  10200.     if (substring(@parameter_name, 1, 1) <> '/' and 
  10201.         substring(@parameter_name, 1, 1) <> '-')
  10202.     BEGIN
  10203.         select @parameter_name = N'-' + @parameter_name
  10204.     END
  10205.  
  10206.     -- Call proc to validate parameter value
  10207.     exec @retcode = dbo.sp_MSvalidate_agent_parameter 
  10208.                         @profile_id = @profile_id, 
  10209.                         @parameter_name = @parameter_name,
  10210.                         @parameter_value = @parameter_value
  10211.     if @retcode <> 0
  10212.         RETURN(1)
  10213.  
  10214.  
  10215.     select @slash_parameter_name = lower(stuff(@parameter_name, 1, 1, N'/') collate SQL_Latin1_General_CP1_CS_AS)
  10216.     select @dash_parameter_name = lower(stuff(@parameter_name, 1, 1, N'-') collate SQL_Latin1_General_CP1_CS_AS)
  10217.      
  10218.  
  10219.     /* A parameter may be defined only once per profile */
  10220.     if exists ( select * from msdb..MSagent_parameters 
  10221.             where profile_id = @profile_id
  10222.             and (lower(parameter_name collate SQL_Latin1_General_CP1_CS_AS) = @slash_parameter_name collate SQL_Latin1_General_CP1_CS_AS  
  10223.             or lower(parameter_name collate SQL_Latin1_General_CP1_CS_AS) = @dash_parameter_name collate SQL_Latin1_General_CP1_CS_AS))
  10224.     BEGIN
  10225.         RAISERROR (20067, 16, -1, @parameter_name)  -- The parameter name ''%s'' already exists for the specified profile
  10226.         RETURN (1)
  10227.     END
  10228.  
  10229.     insert into msdb..MSagent_parameters
  10230.     values (@profile_id, @parameter_name, @parameter_value) ;
  10231.  
  10232.     if @@error <> 0
  10233.         return(1)
  10234. GO
  10235.  
  10236. raiserror('Creating procedure sp_change_agent_parameter', 0,1)
  10237. go
  10238. -- Change one parameter of a profile from the MSagent_parameters table
  10239. create procedure sp_change_agent_parameter(
  10240.     @profile_id int,
  10241.     @parameter_name         sysname,
  10242.     @parameter_value        nvarchar(255)
  10243. )AS
  10244.     declare @slash_parameter_name sysname
  10245.     declare @dash_parameter_name sysname
  10246.     declare @retcode int
  10247.  
  10248.     select @parameter_name = LTRIM(RTRIM(@parameter_name))
  10249.     select @parameter_value = LTRIM(RTRIM(@parameter_value))
  10250.  
  10251.     -- DMO sends in empty sting as '(null)'.
  10252.     if @parameter_value = N'(null)'
  10253.         select @parameter_value = N''
  10254.  
  10255.     -- Append leading '-' when not given
  10256.     if (substring(@parameter_name, 1, 1) <> '/' and 
  10257.         substring(@parameter_name, 1, 1) <> '-')
  10258.     BEGIN
  10259.         select @parameter_name = N'-' + @parameter_name
  10260.     END
  10261.  
  10262.     -- Call proc to validate parameter value
  10263.     exec @retcode = dbo.sp_MSvalidate_agent_parameter 
  10264.                         @profile_id = @profile_id, 
  10265.                         @parameter_name = @parameter_name,
  10266.                         @parameter_value = @parameter_value
  10267.     if @retcode <> 0
  10268.         RETURN(1)
  10269.  
  10270.  
  10271.     select @slash_parameter_name = lower(stuff(@parameter_name, 1, 1, N'/') collate SQL_Latin1_General_CP1_CS_AS)
  10272.     select @dash_parameter_name = lower(stuff(@parameter_name, 1, 1, N'-') collate SQL_Latin1_General_CP1_CS_AS)
  10273.    
  10274.     IF exists ( select * from msdb..MSagent_parameters 
  10275.             where profile_id = @profile_id and 
  10276.                 (lower(parameter_name collate SQL_Latin1_General_CP1_CS_AS) = @slash_parameter_name collate SQL_Latin1_General_CP1_CS_AS or
  10277.                  lower(parameter_name collate SQL_Latin1_General_CP1_CS_AS) = @dash_parameter_name collate SQL_Latin1_General_CP1_CS_AS))
  10278.     BEGIN
  10279.         update msdb..MSagent_parameters set value = @parameter_value where 
  10280.             profile_id = @profile_id and 
  10281.             (lower(parameter_name collate SQL_Latin1_General_CP1_CS_AS) = @slash_parameter_name collate SQL_Latin1_General_CP1_CS_AS or
  10282.             lower(parameter_name collate SQL_Latin1_General_CP1_CS_AS) = @dash_parameter_name collate SQL_Latin1_General_CP1_CS_AS) 
  10283.  
  10284.         if @@error <> 0
  10285.             return(1)
  10286.     END
  10287. GO
  10288.  
  10289. raiserror('Creating procedure sp_change_agent_profile', 0,1)
  10290. go
  10291. -- Change one parameter of a profile from the MSagent_profiles table
  10292. create procedure sp_change_agent_profile(
  10293.     @profile_id         int,
  10294.     @property           sysname,
  10295.     @value              nvarchar(3000)
  10296. )AS
  10297.  
  10298.     IF lower(@property collate SQL_Latin1_General_CP1_CS_AS) not in ('description')
  10299.     BEGIN
  10300.         RAISERROR(21183, 16, -1, @property)
  10301.         RETURN (1)
  10302.     END
  10303.  
  10304.     IF exists ( select * from msdb..MSagent_profiles where profile_id = @profile_id)
  10305.     BEGIN
  10306.         update msdb..MSagent_profiles set description = @value where profile_id = @profile_id
  10307.         if @@error <> 0
  10308.             return(1)
  10309.     END
  10310. GO
  10311.  
  10312.  
  10313. raiserror('Creating procedure sp_help_agent_parameter', 0,1)
  10314. go
  10315. -- View all the parameters of a profile from the MSagent_parameters table
  10316. create procedure sp_help_agent_parameter(
  10317.     @profile_id     int = -1
  10318. )
  10319. as
  10320.     set nocount on
  10321.     
  10322.     if @profile_id = -1
  10323.     begin   
  10324.         select profile_id, parameter_name, value
  10325.         from msdb..MSagent_parameters
  10326.         order by profile_id, parameter_name
  10327.     end
  10328.     else
  10329.     begin
  10330.         select profile_id, parameter_name, value
  10331.         from msdb..MSagent_parameters
  10332.         where profile_id = @profile_id
  10333.         order by profile_id, parameter_name
  10334.     end
  10335. GO
  10336.  
  10337. raiserror('Creating procedure sp_MShelp_distdb', 0,1)
  10338. go
  10339.  
  10340. -- Called by the distribution agent to find the dist. db of a dist. publisher
  10341. create procedure sp_MShelp_distdb (     
  10342.     @publisher_name     sysname
  10343. )
  10344. as
  10345. if exists (select name from msdb..sysobjects where name = 'MSdistpublishers')
  10346. begin
  10347.     select distribution_db from msdb..MSdistpublishers
  10348.     where UPPER(name) = UPPER(@publisher_name) collate database_default
  10349. end
  10350. else 
  10351.     select NULL where 1=2   /* We still want an empty rowset for some agents */
  10352. GO
  10353.  
  10354. dump tran master with no_log
  10355. GO
  10356.  
  10357. raiserror('Creating procedure sp_MSupdate_replication_status', 0,1)
  10358. go
  10359. create procedure sp_MSupdate_replication_status
  10360. @publisher sysname,
  10361. @publisher_db sysname,
  10362. @publication sysname,
  10363. @publication_type int = 0,      -- 0 Transactional/Snapshot 1 Merge
  10364. @agent_type int,
  10365. @agent_name nvarchar(100),
  10366. @status int
  10367. as
  10368.  
  10369.     declare @deleted int
  10370.     declare @refresh int
  10371.     declare @getstatus int
  10372.     declare @dummy int
  10373.     declare @is_merge_agent bit
  10374.     
  10375.     if @agent_type = 4 or @agent_type = 0x80000004
  10376.         set @is_merge_agent = 1
  10377.     else
  10378.         set @is_merge_agent = 0
  10379.  
  10380.     set @deleted = -1
  10381.     set @refresh = -2            -- Status used to only update the timestamp column
  10382.     set @getstatus = -3            -- Get status of agent for dummy distribution row
  10383.  
  10384.  
  10385.     -- if table dne, then we're just installing distribution so we don't need to update status
  10386.     if (select object_id('tempdb.dbo.MSreplication_agent_status')) is NULL
  10387.         return 0
  10388.  
  10389.     -- If there are not rows in the table, we know that it is not loaded or used
  10390.     -- No need to refresh (better performance)
  10391.     -- At least, there will on row if loaded (see sp_MSload_replication_status)
  10392.     if not exists (select * from tempdb.dbo.MSreplication_agent_status)
  10393.         return 0
  10394.  
  10395.     if @status = @getstatus
  10396.     begin
  10397.         select @status = isnull(status, 0) from tempdb.dbo.MSreplication_agent_status where
  10398.             UPPER(publisher) = UPPER(@publisher) and
  10399.             publisher_db = @publisher_db and
  10400.             publication = 'ALL' and
  10401.             agent_type = @agent_type
  10402.     end            
  10403.  
  10404.     -- Update timestamp column via dummy update
  10405.     if @status = @refresh
  10406.     begin
  10407.         -- Dummy update to force timestamps to be updated.
  10408.         update tempdb.dbo.MSreplication_agent_status set status = status where
  10409.             UPPER(publisher) = UPPER(@publisher) and
  10410.             publisher_db = @publisher_db and
  10411.             publication like @publication -- Must use like as publication may be "%"
  10412.         return (0)
  10413.     end
  10414.  
  10415.     -- Remove row if @deleted
  10416.     if @status = @deleted
  10417.     begin
  10418.         if @@trancount > 0
  10419.         begin
  10420.             if exists (select * from tempdb.dbo.MSreplication_agent_status with (TABLOCKX) where 1 = 1)
  10421.             begin
  10422.                 select @dummy = 1
  10423.             end
  10424.         end        
  10425.  
  10426.         if @agent_name = '%' or @agent_name IS NULL
  10427.             delete from tempdb.dbo.MSreplication_agent_status with (TABLOCKX) where
  10428.                 UPPER(publisher) = UPPER(@publisher) and
  10429.                 publisher_db = @publisher_db and
  10430.                 publication = @publication and
  10431.                 agent_type = @agent_type 
  10432.         else
  10433.             delete from tempdb.dbo.MSreplication_agent_status with (TABLOCKX) where
  10434.                 UPPER(publisher) = UPPER(@publisher) and
  10435.                 publisher_db = @publisher_db and
  10436.                 publication = @publication and
  10437.                 agent_type = @agent_type and
  10438.                 agent_name = @agent_name
  10439.  
  10440.         -- Dummy update to force timestamps to be updated.  This will signal a row has been
  10441.         -- removed.
  10442.         update tempdb.dbo.MSreplication_agent_status set status = status where
  10443.             UPPER(publisher) = UPPER(@publisher) and
  10444.             publisher_db = @publisher_db and
  10445.             publication = @publication
  10446.         return (0)
  10447.     end     
  10448.  
  10449.     -- If misc. replication job then the status needs to be mapped.
  10450.     if @agent_type = 5
  10451.     begin
  10452.         set @status = 
  10453.         case isnull(@status,5)    -- mapped to never run
  10454.             when 0 then 5   -- Fail mapping
  10455.             when 1 then 2   -- Success mapping
  10456.             when 2 then 5   -- Retry mapping
  10457.             when 3 then 2   -- Shutdown mapping
  10458.             when 4 then 3   -- Inprogress mapping
  10459.             when 5 then 0   -- Unknown is mapped to never run
  10460.         end
  10461.     end
  10462.  
  10463.     if @is_merge_agent = 1
  10464.     begin
  10465.         update tempdb.dbo.MSreplication_agent_status set status = @status 
  10466.                 where agent_name = @agent_name and
  10467.                 publication = @publication and
  10468.                 UPPER(publisher) = UPPER(@publisher) and
  10469.                 publisher_db = @publisher_db and
  10470.                 agent_type = @agent_type
  10471.  
  10472.         if @@rowcount = 0
  10473.             insert into tempdb.dbo.MSreplication_agent_status 
  10474.                (publisher, publisher_db, publication, publication_type, agent_type, status, agent_name) values
  10475.                (@publisher, @publisher_db, @publication, @publication_type, @agent_type, @status, @agent_name)
  10476.     end
  10477.     else
  10478.     begin
  10479.         if not exists (select * from tempdb.dbo.MSreplication_agent_status where 
  10480.             UPPER(publisher) = UPPER(@publisher) and
  10481.             publisher_db = @publisher_db and
  10482.             publication = @publication and
  10483.             agent_type = @agent_type and
  10484.             agent_name = @agent_name)
  10485.             
  10486.             insert into tempdb.dbo.MSreplication_agent_status 
  10487.                     (publisher, publisher_db, publication, publication_type, agent_type, status, agent_name) values
  10488.                     (@publisher, @publisher_db, @publication, @publication_type, @agent_type, @status, @agent_name)
  10489.                 
  10490.         else
  10491.             update tempdb.dbo.MSreplication_agent_status set status = @status where
  10492.                 UPPER(publisher) = UPPER(@publisher) and
  10493.                 publisher_db = @publisher_db and
  10494.                 (publication = @publication or @publication = 'ALL')and
  10495.                 agent_type = @agent_type and
  10496.                 agent_name = @agent_name
  10497.     end                
  10498.  
  10499.     return (0)
  10500. go
  10501.  
  10502. raiserror('Creating procedure sp_MSenum_misc_agents', 0,1)
  10503. go
  10504. create procedure sp_MSenum_misc_agents
  10505. as
  10506.     SET NOCOUNT ON
  10507.  
  10508.     declare @timestamp timestamp
  10509.  
  10510.     -- Not supported, but returned to be consistent with other sp_MSenum_replication_agent
  10511.     -- result set.
  10512.     set @timestamp = 0
  10513.  
  10514.     select j.name, 'agent_type' = c.name,
  10515.             'status' = case isnull(jh.run_status, 5) -- mapped to never run
  10516.                 when 0 then 5   -- Fail mapping
  10517.                 when 1 then 2   -- Success mapping
  10518.                 when 2 then 5   -- Retry mapping
  10519.                 when 3 then 2   -- Shutdown mapping
  10520.                 when 4 then 3   -- Inprogress mapping
  10521.                 when 5 then 0   -- Unknown is mapped to never run
  10522.             end,
  10523.             jh.message, 'start_time' = convert(nvarchar(8), jh.run_date) + N' ' +
  10524.                                        stuff(stuff(right(convert(nvarchar(7), jh.run_time + 1000000), 6), 5, 0, N':'), 3, 0, N':') + 
  10525.                                        + N'.000',
  10526.             jh.run_duration, 
  10527.             'job_id' = convert(binary(16), j.job_id), 'local_timestamp' = @timestamp from 
  10528.                 msdb..sysjobs j
  10529.             LEFT OUTER JOIN msdb..sysjobhistory jh ON
  10530.             j.job_id = jh.job_id and 
  10531.             jh.instance_id = (select max(instance_id) from msdb..sysjobhistory jh2 where
  10532.                 jh2.job_id = j.job_id)
  10533.             INNER JOIN msdb..syscategories c ON
  10534.             j.category_id = c.category_id
  10535.             where j.category_id in (11, 12,16,17,18)
  10536.  
  10537.     order by j.job_id asc
  10538. go 
  10539.  
  10540. raiserror('Creating procedure sp_MSload_replication_status', 0,1)
  10541. go
  10542.  
  10543. create procedure sp_MSload_replication_status
  10544. as
  10545.     set nocount on
  10546.  
  10547.     declare @db_name sysname
  10548.     declare @distbit int
  10549.     declare @retcode int
  10550.  
  10551.     select @distbit = 16
  10552.     select @retcode = 0
  10553.  
  10554.     -- Flush out all current rows
  10555.     delete tempdb.dbo.MSreplication_agent_status
  10556.  
  10557.     -- Load misc. replication jobs status and map SQL Agent status to replication status
  10558.     -- If users delete all misc repl jobs, the status table may not be updated
  10559.     -- see sp_MSupdate_replication_status
  10560.     insert into tempdb.dbo.MSreplication_agent_status 
  10561.         select '', '', '', -1, 5, 
  10562.             case isnull(jh.run_status,5) -- mapped to never run
  10563.                 when 0 then 5   -- Fail mapping
  10564.                 when 1 then 2   -- Success mapping
  10565.                 when 2 then 5   -- Retry mapping
  10566.                 when 3 then 2   -- Shutdown mapping
  10567.                 when 4 then 3   -- Inprogress mapping
  10568.                 when 5 then 0   -- Unknown is mapped to never run
  10569.             end,
  10570.             j.name, NULL from msdb..sysjobs j
  10571.             LEFT OUTER JOIN msdb..sysjobhistory jh ON
  10572.             j.job_id = jh.job_id and 
  10573.             jh.instance_id = (select max(instance_id) from msdb..sysjobhistory jh2 where
  10574.                 jh2.job_id = j.job_id)
  10575.             where j.category_id in (11,12,16,17,18)
  10576.     if @@error <> 0
  10577.         return 1
  10578.  
  10579.     declare hCdatabase CURSOR LOCAL FAST_FORWARD FOR
  10580.         select name from master.dbo.sysdatabases 
  10581.             where
  10582.             category & @distbit <> 0 and
  10583.             has_dbaccess(name) = 1
  10584.         for read only
  10585.  
  10586.     open hCdatabase
  10587.     fetch next from hCdatabase into @db_name
  10588.     while (@@fetch_status <> -1)
  10589.     begin
  10590.         select @db_name = QUOTENAME(@db_name)
  10591.         
  10592.         insert into tempdb.dbo.MSreplication_agent_status 
  10593.             (publisher, publisher_db, publication, publication_type, agent_type, status, agent_name)
  10594.             exec (@db_name + '.dbo.sp_MSenum_replication_status')
  10595.  
  10596.         fetch next from hCdatabase into @db_name
  10597.     end
  10598.         
  10599. DONE:
  10600.     close hCdatabase
  10601.     deallocate hCdatabase
  10602.  
  10603.  
  10604.     -- Add and initialize Perfmon SQL Replication Agents instances
  10605.     exec @retcode = dbo.sp_MSinit_replication_perfmon
  10606.     if @retcode <> 0 or @@error <> 0
  10607.         return 1
  10608.  
  10609. GO
  10610.  
  10611. raiserror('Creating procedure sp_MScreate_replication_status_table', 0,1)
  10612. go
  10613. create proc sp_MScreate_replication_status_table
  10614. as
  10615.     declare @retcode int
  10616.  
  10617.     if (select object_id('tempdb.dbo.MSreplication_agent_status')) is NULL
  10618.     begin
  10619.         -- begin tran
  10620.         create table tempdb.dbo.MSreplication_agent_status (
  10621.             publisher sysname NOT NULL,
  10622.             publisher_db sysname NOT NULL,
  10623.             publication sysname NOT NULL,
  10624.             publication_type int NOT NULL,          -- 0 transactional/snapshot  1 Merge
  10625.             agent_type int NOT NULL,
  10626.             status int NOT NULL,
  10627.             agent_name nvarchar(100) NOT NULL,
  10628.             timestamp NOT NULL
  10629.             )
  10630.         if @@error <> 0
  10631.             return 1
  10632.         
  10633.           create clustered index cMSreplication_agent_status ON tempdb.dbo.MSreplication_agent_status (agent_name)
  10634.         if @@error <> 0
  10635.             return 1
  10636.         create nonclustered index nc1MSreplication_agent_status ON 
  10637.             tempdb.dbo.MSreplication_agent_status (publication, publisher_db, publisher)
  10638.         if @@error <> 0
  10639.             return 1
  10640.         create nonclustered index nc2MSreplication_agent_status ON 
  10641.             tempdb.dbo.MSreplication_agent_status (agent_type)
  10642.         if @@error <> 0
  10643.             return 1
  10644.         create nonclustered index nc3MSreplication_agent_status ON 
  10645.             tempdb.dbo.MSreplication_agent_status (timestamp)
  10646.         if @@error <> 0
  10647.             return 1
  10648.         exec tempdb.dbo.sp_MS_marksystemobject MSreplication_agent_status
  10649.     end
  10650.     return 0  -- If here, all is well and we're done.
  10651. go
  10652.  
  10653. raiserror('Creating procedure sp_MShelp_replication_status', 0,1)
  10654. go
  10655.  
  10656. create procedure sp_MShelp_replication_status (
  10657.     @publisher sysname = '%',
  10658.     @publisher_db sysname = '%',
  10659.     @publication sysname = '%',
  10660.     @agent_type int = 0,
  10661.     @exclude_anonymous bit = 0
  10662.     )
  10663. as
  10664. begin
  10665.     --set transaction isolation level read uncommitted
  10666.     
  10667.     declare @retcode int
  10668.             ,@max_status int
  10669.             ,@max_timestamp timestamp
  10670.             ,@publication_type int
  10671.             ,@all_status int
  10672.             ,@all_timestamp timestamp
  10673.             ,@distdbname sysname
  10674.             ,@anonymous_mask int
  10675.             ,@anonymous_bit_when_hide int
  10676.             ,@anonymous_bit_when_show int
  10677.             
  10678.     select @anonymous_mask = 0x80000000
  10679.     if @exclude_anonymous = 0
  10680.     begin
  10681.         select @anonymous_bit_when_hide = 0x00000000
  10682.                 ,@anonymous_bit_when_show = 0x80000000
  10683.     end
  10684.     else
  10685.     begin
  10686.         select @anonymous_bit_when_hide = 0x80000000
  10687.                 ,@anonymous_bit_when_show = 0x00000000
  10688.     end
  10689.  
  10690.     -- If the local distributor is not installed, we return nothing rather than
  10691.     -- fail with table not exists error.
  10692.     if (select object_id('tempdb.dbo.MSreplication_agent_status')) is NULL
  10693.         return 0
  10694.  
  10695.     -- If the temporary status table is empty populate it.
  10696.     -- If populated, there should be at lease history cleanup task.
  10697.     if not exists (select * from tempdb.dbo.MSreplication_agent_status)
  10698.     begin                                           
  10699.         exec @retcode = dbo.sp_MSload_replication_status
  10700.         if @@error <> 0
  10701.             return 1
  10702.     end
  10703.  
  10704.     
  10705.     -- @agent_type is only supported with all wildcards
  10706.     if @agent_type <> 0 and (@publisher <> '%' or @publisher_db <> '%' or @publication <> '%')
  10707.         return 1
  10708.     
  10709.     if @publisher = '%'
  10710.     begin
  10711.         -- @agent_type = 0 returns status of all agents including the misc. agents.
  10712.         -- @agent_type = -1 returns status of all agents except misc. agents.
  10713.         if @agent_type = 0 or @agent_type = -1
  10714.         begin
  10715.             select @max_status = max(status), @max_timestamp = max(timestamp) 
  10716.             from tempdb.dbo.MSreplication_agent_status (NOLOCK)
  10717.                 where ((@agent_type = -1 and agent_type <> 5) or
  10718.                       @agent_type = 0) and 
  10719.                       -- Filter out anonymous if needed
  10720.                       agent_type & @anonymous_bit_when_hide = 0
  10721.         end
  10722.         else
  10723.         begin
  10724.             select @max_status = max(status), @max_timestamp = max(timestamp) 
  10725.             from tempdb.dbo.MSreplication_agent_status (NOLOCK) 
  10726.             where
  10727.                 -- Turn off the anonymous bit when showing
  10728.                 agent_type & ~@anonymous_bit_when_show = @agent_type
  10729.         end
  10730.     end
  10731.     else if @publication = '%'
  10732.     begin
  10733.         select @max_status = max(status), @max_timestamp = max(timestamp)
  10734.         from tempdb.dbo.MSreplication_agent_status (NOLOCK) 
  10735.         where UPPER(publisher) = UPPER(@publisher) and 
  10736.               -- Filter out anonymous if needed
  10737.               agent_type & @anonymous_bit_when_hide = 0
  10738. --          publisher_db = @publisher_db 
  10739.  
  10740.         --
  10741.         -- Process the Queue reader entry separately and check if we need to pick them
  10742.         --
  10743.            select @distdbname = distribution_db 
  10744.                 from msdb.dbo.MSdistpublishers where name = @publisher collate database_default
  10745.         
  10746.         select @all_status = status, @all_timestamp = timestamp 
  10747.         from tempdb.dbo.MSreplication_agent_status (NOLOCK) 
  10748.         where agent_type = 9 and publisher = @@servername and publisher_db = @distdbname
  10749.  
  10750.         if @all_status > @max_status
  10751.             select @max_status = @all_status
  10752.  
  10753.         if @all_timestamp > @max_timestamp
  10754.             select @max_timestamp = @all_timestamp
  10755.     end
  10756.     else if @publication <> '%'
  10757.     begin
  10758.         select top 1 @max_status = status from tempdb.dbo.MSreplication_agent_status (NOLOCK) where
  10759.             UPPER(publisher) = UPPER(@publisher) and
  10760.             publisher_db = @publisher_db and
  10761.             publication = @publication  and 
  10762.             -- Filter out anonymous if needed
  10763.             agent_type & @anonymous_bit_when_hide = 0
  10764.             order by status desc
  10765.         
  10766.         -- Get publication type from the Snapshot agent of the Publication. This used to determine
  10767.         -- if the Logreader status should be included.
  10768.         select @publication_type = publication_type from tempdb.dbo.MSreplication_agent_status (NOLOCK) where
  10769.             UPPER(publisher) = UPPER(@publisher) and
  10770.             publisher_db = @publisher_db and
  10771.             publication = @publication and
  10772.             agent_type = 1    -- Snapshot agent
  10773.  
  10774.         select @max_timestamp = max(timestamp) from tempdb.dbo.MSreplication_agent_status (NOLOCK) where
  10775.             UPPER(publisher) = UPPER(@publisher) and
  10776.             publisher_db = @publisher_db and
  10777.             publication = @publication  and 
  10778.             -- Filter out anonymous if needed
  10779.             agent_type & @anonymous_bit_when_hide = 0
  10780.  
  10781.         -- If publication_type = 0 then transactional and must include the Logreader
  10782.         -- with publication = 'ALL'
  10783.         if @publication_type = 0
  10784.         begin
  10785.             select @all_status = status, @all_timestamp = timestamp 
  10786.             from tempdb.dbo.MSreplication_agent_status (NOLOCK) 
  10787.             where
  10788.                 UPPER(publisher) = UPPER(@publisher) and
  10789.                 publisher_db = @publisher_db and
  10790.                 publication = 'ALL' and
  10791.                 agent_type = 2 --Logreader
  10792.  
  10793.             if @all_status > @max_status
  10794.                 select @max_status = @all_status
  10795.  
  10796.             if @all_timestamp > @max_timestamp
  10797.                 select @max_timestamp = @all_timestamp
  10798.  
  10799.             --
  10800.             -- Process the Queue reader entry separately and check if we need to pick them
  10801.             --
  10802.                select @distdbname = distribution_db 
  10803.                     from msdb.dbo.MSdistpublishers where name = @publisher collate database_default
  10804.             
  10805.             declare @proc nvarchar(512)
  10806.                     ,@allow_queued_tran bit
  10807.                     
  10808.             SELECT @proc = quotename(@distdbname) + N'.dbo.sp_MSispublicationqueued'
  10809.             exec @retcode = @proc
  10810.                 @publisher = @publisher
  10811.                 ,@publisher_db = @publisher_db
  10812.                 ,@publication = @publication
  10813.                 ,@allow_queued_tran = @allow_queued_tran output
  10814.             if (@retcode != 0 and @@error != 0)
  10815.                 return 1
  10816.  
  10817.             if (@allow_queued_tran = 1)
  10818.             begin
  10819.                 select @all_status = status, @all_timestamp = timestamp
  10820.                 from tempdb.dbo.MSreplication_agent_status (NOLOCK) 
  10821.                 where agent_type = 9 and publisher = @@servername and publisher_db = @distdbname
  10822.  
  10823.                 if @all_status > @max_status
  10824.                     select @max_status = @all_status 
  10825.  
  10826.                 if @all_timestamp > @max_timestamp
  10827.                     select @max_timestamp = @all_timestamp
  10828.             end
  10829.         end
  10830.     end
  10831.  
  10832.     -- Return result set
  10833.     select 'status' = isnull(@max_status, 0), 'timestamp' = isnull (@max_timestamp, 0)
  10834.  
  10835.     --set transaction isolation level read committed
  10836.     return (0)
  10837. end
  10838. go
  10839.  
  10840. raiserror('Creating procedure sp_MSenum_replication_agents', 0,1)
  10841. go
  10842. create procedure sp_MSenum_replication_agents
  10843. @type int,
  10844. @exclude_anonymous bit = 0,
  10845. @check_user bit = 0
  10846. as
  10847.     set nocount on
  10848.     --set transaction isolation level read uncommitted
  10849.  
  10850.     declare @snapshot int
  10851.     declare @logreader int
  10852.     declare @distribution int
  10853.     declare @sproc sysname
  10854.     declare @db_name sysname
  10855.     declare @table_name sysname
  10856.     declare @cmd nvarchar(255)
  10857.     declare @distbit int
  10858.     declare @merge int
  10859.     declare @misc int
  10860.     declare @qreader int
  10861.  
  10862.     select @distbit = 16
  10863.  
  10864.     select @snapshot = 1
  10865.     select @logreader = 2
  10866.     select @distribution = 3
  10867.     select @merge = 4
  10868.     select @misc = 5
  10869.     select @qreader = 9
  10870.  
  10871.     if @type = @misc
  10872.     begin
  10873.         exec dbo.sp_MSenum_misc_agents
  10874.         return 0
  10875.     end
  10876.  
  10877.     if @type = @snapshot
  10878.     begin
  10879.         select @sproc = 'sp_MSenum_snapshot'
  10880.         create table #snapshot (dbname sysname collate database_default not null, name nvarchar(100) collate database_default not null, 
  10881.             status int NOT NULL,
  10882.             publisher sysname collate database_default not null, publisher_db sysname collate database_default not null, 
  10883.             publication sysname collate database_default not null,
  10884.             start_time nvarchar(24) collate database_default null, time nvarchar(24) collate database_default null, duration int NULL,
  10885.             comments nvarchar(255) collate database_default null, delivered_transactions int NULL, 
  10886.             delivered_commands int NULL, delivery_rate float NULL, error_id INT NULL,
  10887.             job_id binary(16) NULL, local_job bit NULL, profile_id int NOT NULL, 
  10888.             agent_id int NOT NULL, local_timestamp binary(8) NOT NULL)
  10889.  
  10890.         create unique clustered index ucsnapshot ON #snapshot (job_id)
  10891.  
  10892.         select @table_name = '#snapshot'
  10893.     end
  10894.     else if @type = @logreader
  10895.     begin
  10896.         select @sproc = 'sp_MSenum_logreader'
  10897.         create table #logreader (dbname sysname collate database_default not null, name nvarchar(100) collate database_default not null,  
  10898.             status int NOT NULL,
  10899.             publisher sysname collate database_default not null, publisher_db sysname collate database_default not null,
  10900.             start_time nvarchar(24) collate database_default null, time nvarchar(24) collate database_default null, duration int NULL,
  10901.             comments nvarchar(255) collate database_default null, delivery_time int NULL, 
  10902.             delivered_transactions int NULL, delivered_commands int NULL, 
  10903.             average_commands int NULL, delivery_rate int NULL, delivery_latency int NULL, 
  10904.             error_id INT NULL, job_id binary(16) NULL, local_job bit NULL,
  10905.             profile_id int NOT NULL, agent_id int NOT NULL, local_timestamp binary(8) NOT NULL)
  10906.  
  10907.         create unique clustered index uclogreader ON #logreader (job_id)
  10908.  
  10909.         select @table_name = '#logreader'
  10910.     end
  10911.     else if @type = @distribution
  10912.     begin
  10913.         select @sproc = 'sp_MSenum_distribution'
  10914.         create table #distribution (dbname sysname collate database_default not null, 
  10915.             name nvarchar(100) collate database_default not null,  
  10916.             status int NOT NULL,
  10917.             publisher sysname collate database_default not null, publisher_db sysname collate database_default not null, 
  10918.             publication sysname collate database_default null,
  10919.             subscriber sysname collate database_default null, subscriber_db sysname collate database_default null, subscription_type int NULL,
  10920.             start_time nvarchar(24) collate database_default null, time nvarchar(24) collate database_default null, duration int NULL,
  10921.             comments nvarchar(255) NULL, delivery_time int NULL, 
  10922.             delivered_transactions int NULL, delivered_commands int NULL, 
  10923.             average_commands int NULL, delivery_rate int NULL, 
  10924.             delivery_latency int NULL, error_id INT NULL,
  10925.             job_id binary(16) NULL, local_job bit NULL, profile_id int NOT NULL, 
  10926.             agent_id int NOT NULL, local_timestamp binary(8) NOT NULL, 
  10927.             offload_enabled bit NOT NULL, offload_server sysname collate database_default null,
  10928.             subscriber_type tinyint NULL)
  10929.  
  10930.         create unique clustered index ucdistribution ON #distribution (job_id)
  10931.  
  10932.         select @table_name = '#distribution'
  10933.     end
  10934.     else if @type = @merge
  10935.     begin
  10936.         select @sproc = 'sp_MSenum_merge'
  10937.         create table #merge (dbname sysname collate database_default not null, name nvarchar(100) collate database_default not null, 
  10938.             status int NOT NULL,
  10939.             publisher sysname collate database_default not null, publisher_db sysname collate database_default not null, publication sysname collate database_default null,
  10940.             subscriber sysname collate database_default null, subscriber_db sysname collate database_default null, subscription_type int NULL,
  10941.             start_time nvarchar(24) collate database_default null, time nvarchar(24) collate database_default null, duration int NULL, 
  10942.             comments nvarchar(255) NULL, delivery_rate int NULL,
  10943.             publisher_insertcount int NULL, publisher_updatecount int NULL, publisher_deletecount int NULL,
  10944.             publisher_conficts int NULL, 
  10945.             subscriber_insertcount int NULL, subscriber_updatecount int NULL, subscriber_deletecount int NULL,
  10946.             subscriber_conficts int NULL, error_id int NULL, job_id binary(16) NULL,
  10947.             local_job bit NULL, profile_id int NOT NULL, agent_id int NOT NULL, 
  10948.             local_timestamp binary(8) NOT NULL, offload_enabled bit NOT NULL, 
  10949.             offload_server sysname collate database_default null, subscriber_type tinyint NULL)
  10950.  
  10951.         create unique clustered index ucmerge ON #merge (job_id)
  10952.  
  10953.  
  10954.         select @table_name = '#merge'
  10955.     end
  10956.     else if @type = @qreader
  10957.     begin
  10958.         select @sproc = 'sp_MSenum_qreader'
  10959.         create table #qreader (dbname sysname collate database_default not null, 
  10960.             name nvarchar(100) collate database_default not null, status int NOT NULL,
  10961.             start_time nvarchar(24) collate database_default null, time nvarchar(24) collate database_default null, duration int NULL,
  10962.             comments nvarchar(255) collate database_default null, 
  10963.             transactions_processed int NULL, commands_processed int NULL, 
  10964.             average_commands int NULL, delivery_rate int NULL, delivery_latency int NULL,
  10965.             error_id INT NULL, job_id binary(16) NULL, 
  10966.             profile_id int NULL, agent_id int NOT NULL, local_timestamp binary(8) NOT NULL)
  10967.  
  10968.         create unique clustered index ucqreader ON #qreader (job_id)
  10969.  
  10970.         select @table_name = '#qreader'
  10971.     end
  10972.  
  10973.     declare hCdatabase CURSOR LOCAL FAST_FORWARD FOR
  10974.         select name from master.dbo.sysdatabases 
  10975.             where
  10976.             category & @distbit <> 0 and
  10977.             has_dbaccess(name) = 1
  10978.     for read only
  10979.  
  10980.     open hCdatabase
  10981.     fetch next from hCdatabase into @db_name
  10982.     while (@@fetch_status <> -1)
  10983.     begin
  10984.         
  10985.         -- Check to see if the user has permision to monitor the distribution 
  10986.         -- database.
  10987.         declare @has_pm bit
  10988.         select @cmd = quotename(@db_name) + '.dbo.sp_executesql'
  10989.         exec @cmd
  10990.             N'if is_member(N''db_owner'') = 1 or is_member(N''replmonitor'') = 1 set @has_pm = 1', 
  10991.             N'@has_pm bit output',
  10992.             @has_pm output
  10993.         if @has_pm = 1
  10994.         begin
  10995.             if @check_user = 1
  10996.             begin
  10997.                 select 'is_replmonitor' = 1
  10998.                 return
  10999.             end
  11000.             else
  11001.             begin
  11002.                 select @cmd = 'insert into ' + @table_name +
  11003.                     ' exec ' + quotename(@db_name) + '.dbo.' + @sproc + ' @show_distdb = 1' 
  11004.                     
  11005.                 if @type in (@distribution, @merge)
  11006.                     select @cmd = @cmd +
  11007.                         ', @exclude_anonymous = ' + cast(@exclude_anonymous as nvarchar(1))
  11008.  
  11009.                 exec (@cmd)
  11010.             end
  11011.         end
  11012.         fetch next from hCdatabase into @db_name
  11013.     end
  11014.     close hCdatabase
  11015.     deallocate hCdatabase
  11016.  
  11017.     -- Don't return agents if in check user mode
  11018.     if @check_user = 1
  11019.         return
  11020.  
  11021.  
  11022.     select @cmd = 'select * from ' + @table_name + ' order by job_id asc'
  11023.     exec (@cmd)
  11024.  
  11025.     -- Drop the table
  11026.     select @cmd = 'drop table ' + @table_name
  11027.     exec (@cmd)
  11028.     --set transaction isolation level read committed
  11029.  
  11030. GO
  11031.  
  11032. raiserror('Creating procedure sp_replication_agent_checkup', 0,1)
  11033. go
  11034. create procedure sp_replication_agent_checkup
  11035. @heartbeat_interval int = 10        -- minutes
  11036. as
  11037.     declare @distribution_db sysname
  11038.     declare @retstatus int
  11039.     declare @proc nvarchar(255)
  11040.     declare @retcode int
  11041.  
  11042.     declare hCdistdbs CURSOR LOCAL FAST_FORWARD for
  11043.         select name from msdb..MSdistributiondbs where 
  11044.             has_dbaccess(name) = 1
  11045.     for read only
  11046.     open hCdistdbs
  11047.     fetch hCdistdbs into @distribution_db
  11048.     while @@fetch_status <> -1
  11049.     begin
  11050.         select @proc = @distribution_db + '.dbo.sp_MSagent_stethoscope'
  11051.         execute  @retcode = @proc @heartbeat_interval
  11052.         if @@error <> 0 or @retcode <> 0
  11053.         begin
  11054.             select @retstatus = 1
  11055.             goto UNDO
  11056.         end
  11057.         fetch hCdistdbs into @distribution_db
  11058.     end
  11059.  
  11060.     set @retstatus = 0
  11061.  
  11062. UNDO:
  11063.     close hCdistdbs
  11064.     deallocate hCdistdbs
  11065.     return (@retstatus)
  11066. go
  11067.  
  11068. raiserror('Creating procedure sp_MScreate_replication_checkup_agent', 0,1)
  11069. go
  11070. create procedure sp_MScreate_replication_checkup_agent
  11071. @heartbeat_interval int = 10    -- minutes
  11072. as
  11073.     declare @command nvarchar(100)
  11074.     declare @retcode int
  11075.     declare @interval int
  11076.     declare @job_name nvarchar(100)
  11077.     declare @description nvarchar(100)
  11078.     declare @category_name sysname
  11079.  
  11080.     select @command = 'sp_replication_agent_checkup @heartbeat_interval = ' +
  11081.         convert(nvarchar(10), @heartbeat_interval)      
  11082.         
  11083.     -- Create job if it already exists
  11084.     select @job_name = formatmessage(20533)
  11085.  
  11086.     IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @job_name collate database_default and
  11087.         UPPER(originating_server) = UPPER(CONVERT(sysname, SERVERPROPERTY('ServerName'))))
  11088.     BEGIN
  11089.         EXEC @retcode = msdb.dbo.sp_delete_job  @job_name = @job_name
  11090.         IF @@ERROR <> 0 or @retcode <> 0
  11091.         BEGIN
  11092.             return (1)            
  11093.         END
  11094.     END
  11095.  
  11096.     -- Create new job
  11097.     set @interval = convert(int, @heartbeat_interval)
  11098.     set @description = formatmessage(20534)
  11099.  
  11100.     -- Get Checkup category name (assumes category_id = 16)
  11101.     select @category_name = name FROM msdb.dbo.syscategories where category_id = 16
  11102.     EXECUTE @retcode = dbo.sp_MSadd_repl_job 
  11103.             @name = @job_name,
  11104.             @subsystem = 'TSQL', 
  11105.             @enabled = 1, 
  11106.             @command = @command,
  11107.             @description = @description,
  11108.             @freqtype = 4,
  11109.             @freqinterval = 1,
  11110.             @freqsubtype = 4,
  11111.             @freqsubinterval = @interval,
  11112.             @retryattempts = 0,
  11113.             @category_name = @category_name
  11114.     if @@ERROR <> 0 or @retcode <> 0
  11115.         return (1)
  11116. go
  11117.  
  11118. raiserror('Creating procedure sp_MSenum_replication_job', 0,1)
  11119. go
  11120. create procedure sp_MSenum_replication_job
  11121. @job_id uniqueidentifier
  11122. as
  11123.     SET NOCOUNT ON
  11124.     declare @message nvarchar(1024)
  11125.     declare @retcode int
  11126.     declare @runstatus int
  11127.     declare @date int
  11128.     declare @time int
  11129.  
  11130.     -- Get last message from SQL Agent History table
  11131.     create table #JobHistory (
  11132.         instance_id int NOT NULL, 
  11133.         job_id uniqueidentifier NOT NULL,
  11134.         job_name nvarchar(100) collate database_default not null,
  11135.         step_id int NOT NULL,
  11136.         step_name nvarchar(100) collate database_default not null, 
  11137.         sql_message_id int NOT NULL,
  11138.         sql_severity int NOT NULL,
  11139.         message nvarchar(1024) collate database_default null,
  11140.         run_status int NOT NULL,
  11141.         run_date int NOT NULL,
  11142.         run_time int NOT NULL,
  11143.         run_duration int NOT NULL,
  11144.         operator_emailed sysname collate database_default null,
  11145.         operator_netsent sysname collate database_default null,
  11146.         operator_paged sysname collate database_default null,
  11147.         retries_attempted int NOT NULL,
  11148.         server sysname collate database_default not null
  11149.     )
  11150.     if @@error <> 0
  11151.         return 1
  11152.  
  11153.     -- Insert last history for step_id 1 (Agent running)
  11154.     set rowcount 1
  11155.     insert into #JobHistory exec msdb.dbo.sp_help_jobhistory @job_id = @job_id, @step_id = 1, 
  11156.         @mode = 'FULL'          
  11157.  
  11158.     -- Get the last history
  11159.     select @message = message, @runstatus = run_status, @date = run_date, @time = run_time
  11160.         from #JobHistory
  11161.  
  11162.     -- Reset rowcount
  11163.     set rowcount 0
  11164.  
  11165.     -- Map SQL Agent runstatus to Replication runstatus
  11166.     set @runstatus = 
  11167.     case @runstatus
  11168.         when 0 then 6   -- Fail mapping
  11169.         when 1 then 2   -- Success mapping
  11170.         when 2 then 5   -- Retry mapping
  11171.         when 3 then 2   -- Shutdown mapping
  11172.         when 4 then 3   -- Inprogress mapping
  11173.         when 5 then 0   -- Unknown is mapped to never run
  11174.     end
  11175.  
  11176.     declare @sztime nchar(6)
  11177.     select @sztime = convert(nchar(6), @time)
  11178.  
  11179.     -- If the time is like 53030 (5:30:30), make it 053030
  11180.     if len(@sztime) = 5
  11181.         select @sztime = N'0' + @sztime
  11182.  
  11183.     -- Return status and message
  11184.     select 'runstatus' = isnull(@runstatus, 0), 'message' = @message,  'date' = @date, 
  11185.         'time' = @time, 'datetime' = 
  11186.             -- use this style 20000405 13:05:00.000, refer to sp_MSenumallsubscriptions
  11187.             convert(nchar(8), @date) + N' ' + 
  11188.             substring(@sztime, 1, 2) + N':' +
  11189.             substring(@sztime, 3, 2) + N':' +
  11190.             -- miliseconds are not availible
  11191.             substring(@sztime, 5, 2) + N'.000'
  11192.  
  11193.     drop table #JobHistory
  11194. go 
  11195.  
  11196. raiserror('Creating procedure sp_MSrepl_dbrole', 0,1)
  11197. go
  11198. CREATE PROCEDURE sp_MSrepl_dbrole
  11199.     @rolename       sysname,
  11200.     @loginname      sysname,
  11201.     @operation      nvarchar(10),
  11202.     @is_member      bit = 0 OUTPUT
  11203. AS
  11204.     -- This is an internal stored procedure. 
  11205.     -- If operation is 'add', it will add the login as a user to current database if necessary
  11206.     -- get the user name of the login and add it to the role
  11207.     -- If operation is 'drop', it will drop the user from the role.
  11208.     -- SETUP RUNTIME OPTIONS / DECLARE VARIABLES --
  11209.     declare @retcode int
  11210.     declare @username sysname
  11211.     declare @sid varbinary(85)
  11212.  
  11213.     select @sid = suser_sid(@loginname)
  11214.  
  11215.     if @operation = 'is_member'
  11216.     begin
  11217.         if exists (select * from sysusers r, sysusers u, sysmembers m where 
  11218.             u.sid = @sid and
  11219.             r.name = @rolename and
  11220.             m.groupuid = r.uid and
  11221.             m.memberuid = u.uid)
  11222.             select @is_member = 1
  11223.         else
  11224.             select @is_member = 0
  11225.         return(0)
  11226.     end
  11227.     
  11228.     -- Add the login to  db  role.
  11229.     if  is_srvrolemember('sysadmin',  @loginname) <> 1
  11230.     begin
  11231.         select @username = name from sysusers where sid = @sid
  11232.         if @operation = 'add'
  11233.         begin
  11234.             if @username is null
  11235.             begin
  11236.                 -- Add the server login to be a user with same name in the database
  11237.                 exec @retcode = dbo.sp_adduser  @loginname
  11238.                 if @retcode<>0 or @@error <> 0
  11239.                     return 1
  11240.                 select @username = @loginname
  11241.             end
  11242.  
  11243.             exec @retcode = dbo.sp_addrolemember @rolename, @username
  11244.             if @@error <> 0 OR @retcode <> 0
  11245.                 return 1
  11246.         end
  11247.         else if @operation = 'drop'
  11248.         begin
  11249.             if @username is not null
  11250.             begin
  11251.                 exec @retcode = dbo.sp_droprolemember @rolename, @username
  11252.                 if @@error <> 0 OR @retcode <> 0
  11253.                     return 1
  11254.             end
  11255.         end
  11256.     end     
  11257. go
  11258.  
  11259. raiserror('Creating procedure sp_changedistributor_password', 0,1)
  11260. go
  11261. CREATE PROCEDURE sp_changedistributor_password (
  11262.     @password sysname       
  11263.     ) AS
  11264.  
  11265.     declare @distributor sysname
  11266.     declare @distnetname sysname
  11267.     declare @retcode int
  11268.     declare @login sysname
  11269.  
  11270.     select @login = 'distributor_admin'
  11271.     SELECT @distributor = NULL
  11272.     SELECT @distributor = srvname, @distnetname = datasource 
  11273.         FROM master..sysservers
  11274.         WHERE  srvstatus & 8 <> 0
  11275.     
  11276.     exec @retcode = dbo.sp_addlinkedsrvlogin 
  11277.         @rmtsrvname= @distributor,
  11278.         @useself = 'false',
  11279.         @locallogin = NULL,
  11280.         @rmtuser = @login,
  11281.         @rmtpassword = @password
  11282.     IF @@error <> 0 OR @retcode <> 0
  11283.     BEGIN
  11284.         RETURN (1)
  11285.     END
  11286.  
  11287.     if UPPER(@distnetname) = UPPER(@@servername)
  11288.     begin
  11289.             -- Change the password if the distributor is local
  11290.         EXEC @retcode = dbo.sp_password NULL, @password, 'distributor_admin'
  11291.         if @@error <> 0 or @retcode <> 0
  11292.             return(1)
  11293.     end
  11294.  
  11295.     return (0)
  11296. GO
  11297.  
  11298. raiserror('Creating procedure sp_oledbinfo', 0,1)
  11299. go
  11300. CREATE PROCEDURE sp_oledbinfo
  11301.     @server nvarchar(128),
  11302.     @infotype nvarchar(128) = NULL,
  11303.     @login nvarchar(128) = NULL,
  11304.     @password nvarchar(128) = NULL
  11305.     AS
  11306.  
  11307.     SET NOCOUNT ON
  11308.  
  11309.     DECLARE @distributor sysname
  11310.     DECLARE @distproc nvarchar (255)
  11311.     DECLARE @retcode int
  11312.  
  11313.     /*
  11314.     ** Get distribution server information for remote RPC
  11315.     ** subscription calls.
  11316.     */
  11317.  
  11318.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT
  11319.     IF @@error <> 0 OR @retcode <> 0
  11320.         BEGIN
  11321.         RAISERROR (14071, 16, -1)
  11322.             RETURN (1)
  11323.     END
  11324.  
  11325.     select @distproc = rtrim(@distributor) + '.master.dbo.sp_MSget_oledbinfo'
  11326.     exec @retcode = @distproc @server, @infotype, @login, @password
  11327.     IF @@error <> 0
  11328.         BEGIN
  11329.         RAISERROR (14071, 16, -1)
  11330.         RETURN (1)
  11331.     END
  11332.  
  11333. go
  11334.  
  11335. raiserror('Creating procedure sp_MSget_oledbinfo', 0,1)
  11336. go
  11337. CREATE PROCEDURE sp_MSget_oledbinfo
  11338.     @server nvarchar(128),  -- the name by which the oledb datasource is referred to.
  11339.     @infotype nvarchar(128) = NULL,
  11340.     @login nvarchar(128) = NULL,
  11341.     @password nvarchar(128) = NULL
  11342.     AS
  11343.  
  11344.     SET NOCOUNT ON
  11345.  
  11346.     DECLARE @distproc nvarchar (255)
  11347.     DECLARE @providername nvarchar(256)
  11348.     DECLARE @datasource nvarchar(4000)
  11349.     DECLARE @location nvarchar(4000)
  11350.     DECLARE @providerstring nvarchar(4000)
  11351.     DECLARE @catalog nvarchar(256)
  11352.     DECLARE @retcode int
  11353.  
  11354.     select @providername = providername, @datasource = datasource, @location = location, @providerstring = providerstring, @catalog = catalog
  11355.         from master..sysservers where UPPER(srvname) = UPPER(@server) collate database_default
  11356.  
  11357.     if (@@rowcount = 0)
  11358.     begin
  11359.         raiserror(15015, 16, -1, @server)
  11360.         return (1)
  11361.     end
  11362.  
  11363.     exec @retcode = master..xp_oledbinfo @providername, @datasource, @location, @providerstring, @catalog, @login, @password, @infotype
  11364.     IF @@error <> 0
  11365.         BEGIN
  11366.         RAISERROR (14071, 16, -1)
  11367.         RETURN (1)
  11368.     END
  11369.  
  11370. go
  11371.  
  11372.  
  11373. raiserror('Creating procedure sp_grant_publication_access', 0,1)
  11374. go
  11375. CREATE PROCEDURE sp_grant_publication_access (
  11376.     @publication sysname,
  11377.     @login sysname,
  11378.     @reserved nvarchar(10) = NULL
  11379.         ) AS
  11380.  
  11381.     -- This stored procedure can be called repeatedly.
  11382.     DECLARE @distribdb sysname
  11383.     DECLARE @distproc nvarchar (300)
  11384.     DECLARE @retcode int
  11385.     DECLARE @dist_rpcname sysname
  11386.     DECLARE @database sysname
  11387.     
  11388.     -- Security Check
  11389.     exec @retcode = dbo.sp_MSreplcheck_publish
  11390.     if @@ERROR <> 0 or @retcode <> 0
  11391.         return(1)
  11392.     
  11393.     select @database = db_name()
  11394.  
  11395.     -- Existance check of the publication will be done in sp_MSpublication_access
  11396.     -- Note, even if the login exists, it may or may not has access
  11397.     -- to the server (granted or denied). 
  11398.     if @reserved is null
  11399.     begin
  11400.         if not exists (select * from master..syslogins where 
  11401.             sid = suser_sid(@login) and
  11402.             hasaccess = 1)
  11403.         begin
  11404.             raiserror(15007, 16, -1, @login) 
  11405.             return (1)
  11406.         end
  11407.         
  11408.         -- Don't do the user check if sysadmin since sysadmin can enter
  11409.         -- the database as dbo.
  11410.         if is_srvrolemember('sysadmin',@login) = 0 and
  11411.             not exists (select * from sysusers where
  11412.             (sid = suser_sid(@login) or name = N'guest') and
  11413.             hasdbaccess = 1) 
  11414.         begin
  11415.             raiserror(20619, 16, -1, @login, @database) 
  11416.             return (1)            
  11417.         end
  11418.     end
  11419.  
  11420.     -- at this point we know that the login exists and has access to the server
  11421.     -- get the pubid and add the given login to the PAL role
  11422.     -- don't add role if 'init' because role will be added in addmergepublication
  11423.     declare @role sysname
  11424.     declare @pubid uniqueidentifier
  11425.     declare @pubidstr nvarchar(40)
  11426.     declare @user sysname
  11427.  
  11428.     if (@reserved <> 'init')
  11429.     begin
  11430.         if exists(select * from dbo.sysobjects where name = 'sysmergepublications')
  11431.         begin
  11432.             select @pubid = pubid from dbo.sysmergepublications where name = @publication and UPPER(publisher) = UPPER(@@servername) collate database_default and publisher_db = db_name()
  11433.             if (@pubid is not null)
  11434.             begin
  11435.                 -- Fix the databse role which represents the users who have access to this publication
  11436.                 -- this has to be outside the transaction. Hence it is done here since adding the role 
  11437.                 -- could not be done in sp_addmergepublication.
  11438.                 exec @retcode = dbo.sp_MSrepl_FixPALRole @pubid, @role output
  11439.                 if (@retcode<>0 or @@error <> 0)
  11440.                     return 1
  11441.  
  11442.                 -- map the login to a user in the current db
  11443.                 select @user = name from sysusers where sid = suser_sid(@login) and hasdbaccess = 1
  11444.                 if @user is NULL
  11445.                 begin
  11446.                     if (is_srvrolemember('sysadmin', @login) = 1) 
  11447.                         select @user = 'dbo'
  11448.  
  11449.                     if @user is NULL
  11450.                     begin
  11451.                         raiserror(20624, 16, -1, @login, @database) 
  11452.                         return (1)
  11453.                     end
  11454.                 end
  11455.  
  11456.                 if @user <> 'dbo'
  11457.                 begin
  11458.                     exec @retcode = dbo.sp_addrolemember @role, @user
  11459.                     if (@retcode <> 0 or @@error <> 0)
  11460.                         return 1
  11461.                 end
  11462.             end
  11463.         end
  11464.     end
  11465.     
  11466.     EXEC @retcode = dbo.sp_helpdistributor  @rpcsrvname = @dist_rpcname OUTPUT,
  11467.                                         @distribdb   = @distribdb   OUTPUT
  11468.     IF @@error <> 0
  11469.     BEGIN
  11470.          RAISERROR (14071, 16, -1)
  11471.          return (1)
  11472.     END
  11473.  
  11474.     IF @retcode <> 0 OR @distribdb IS NULL OR @dist_rpcname IS NULL
  11475.     BEGIN
  11476.         RAISERROR (14071, 16, -1)
  11477.         return(1)
  11478.     END
  11479.  
  11480.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSpublication_access'
  11481.     
  11482.     declare @skip bit
  11483.  
  11484.     declare @login2 sysname
  11485.  
  11486.     if @reserved = 'init'
  11487.     begin
  11488.         -- Skip logins that are not at the distributor without
  11489.         -- raiseing error.
  11490.         set @skip = 1
  11491.         declare hC CURSOR LOCAL FAST_FORWARD for
  11492.             select loginname from master..syslogins where
  11493.                 (is_srvrolemember('sysadmin', loginname) = 1 or
  11494.                 sid = suser_sid()) 
  11495.         for read only
  11496.     end
  11497.     else
  11498.     begin
  11499.         set @skip = 0
  11500.         declare hC CURSOR LOCAL FAST_FORWARD for
  11501.                 select @login
  11502.             for read only
  11503.     end
  11504.  
  11505.     open hC
  11506.     fetch hC into @login2
  11507.  
  11508.     while (@@fetch_status <> -1)
  11509.     begin
  11510.         EXEC @retcode = @distproc
  11511.              @publisher = @@SERVERNAME,
  11512.              @publisher_db = @database,
  11513.              @publication = @publication,
  11514.              @login= @login2,
  11515.              @operation = 'add',
  11516.              @skip = @skip
  11517.         IF @@error <> 0 OR @retcode <> 0
  11518.             return (1)
  11519.  
  11520.         fetch hC into @login2
  11521.     end
  11522.     
  11523.     close hC
  11524.     deallocate hC
  11525. GO
  11526.  
  11527. raiserror('Creating procedure sp_revoke_publication_access', 0,1)
  11528. go
  11529. CREATE PROCEDURE sp_revoke_publication_access (
  11530.     @publication sysname,
  11531.     @login sysname
  11532.         ) AS
  11533.  
  11534.     -- This stored procedure can be called repeatedly.
  11535.     DECLARE @distribdb sysname
  11536.     DECLARE @distproc nvarchar (300)
  11537.     DECLARE @retcode int
  11538.     DECLARE @dist_rpcname sysname
  11539.     DECLARE @database sysname
  11540.  
  11541.     -- Security Check
  11542.     exec @retcode = dbo.sp_MSreplcheck_publish
  11543.     if @@ERROR <> 0 or @retcode <> 0
  11544.         return(1)
  11545.     -- remove the user from the PAL role in the publishing database
  11546.     -- added for role based security
  11547.     declare @role sysname
  11548.     declare @pubid uniqueidentifier
  11549.     declare @pubidstr nvarchar(40)
  11550.     declare @user sysname
  11551.     
  11552.     if exists(select * from dbo.sysobjects where name = 'sysmergepublications')
  11553.     begin
  11554.         select @pubid = pubid from dbo.sysmergepublications where name = @publication and UPPER(publisher) = UPPER(@@servername) collate database_default and publisher_db = db_name()
  11555.         if (@pubid is not null)
  11556.         begin
  11557.             exec dbo.sp_MSguidtostr @pubid, @pubidstr output
  11558.             set @role = 'MSmerge_' + @pubidstr
  11559.  
  11560.             if exists (select * from dbo.sysusers where issqlrole=1 and name=@role)
  11561.             begin
  11562.                 select @user = name from sysusers where sid = suser_sid(@login) and name <> 'dbo'
  11563.                 if @user is not NULL
  11564.                 begin
  11565.                     exec @retcode = dbo.sp_droprolemember @role, @user
  11566.                     if (@retcode <> 0 or @@error <> 0)
  11567.                         return 1
  11568.                 end
  11569.             end
  11570.         end
  11571.     end
  11572.  
  11573.  
  11574.     -- Do check existense when dropping since the login might be dropped
  11575.     -- outside replication already.
  11576.  
  11577.     select @database = db_name()
  11578.  
  11579.     EXEC @retcode = dbo.sp_helpdistributor  @rpcsrvname = @dist_rpcname OUTPUT,
  11580.                                         @distribdb   = @distribdb   OUTPUT
  11581.     IF @@error <> 0
  11582.     BEGIN
  11583.          RAISERROR (14071, 16, -1)
  11584.          return (1)
  11585.     END
  11586.  
  11587.     IF @retcode <> 0 OR @distribdb IS NULL OR @dist_rpcname IS NULL
  11588.     BEGIN
  11589.         RAISERROR (14071, 16, -1)
  11590.         return(1)
  11591.     END
  11592.  
  11593.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSpublication_access'
  11594.     EXEC @retcode = @distproc
  11595.          @publisher = @@SERVERNAME,
  11596.          @publisher_db = @database,
  11597.          @publication = @publication,
  11598.          @login= @login,
  11599.          @operation = 'drop'
  11600.  
  11601.     IF @@error <> 0 OR @retcode <> 0
  11602.         return (1)
  11603.  
  11604.     -- also remove the entry in tempdb cache on the publisher. this entry is created when calling replcheck_pull and 
  11605.     -- is subsequently used by replcheck_connection. By removing all cache entries for this publication
  11606.     -- there would be a small performance hit but this would be the best way to fix the security hole since
  11607.     -- the login could be role or an NT group
  11608.     delete from tempdb.dbo.MSpublisher_access 
  11609.         where publication = @publication and db_id = db_id()
  11610.     if @@error <> 0
  11611.         return (1)
  11612.  
  11613. GO
  11614.  
  11615. raiserror('Creating procedure sp_help_publication_access', 0,1)
  11616. go
  11617. CREATE PROCEDURE sp_help_publication_access (
  11618.     @publication sysname,
  11619.     @return_granted bit = 1,
  11620.     @login sysname = '%',
  11621.     @initial_list bit = 0
  11622.         ) AS
  11623.  
  11624.     set nocount on
  11625.     -- This stored procedure can be called repeatedly.
  11626.     DECLARE @distribdb sysname
  11627.     DECLARE @distproc nvarchar (300)
  11628.     DECLARE @retcode int
  11629.     DECLARE @dist_rpcname sysname
  11630.     DECLARE @database sysname
  11631.  
  11632.     exec @retcode = dbo.sp_MSreplcheck_publish
  11633.     if @@ERROR <> 0 or @retcode <> 0
  11634.         return(1)
  11635.  
  11636.     -- Do check existense when dropping since the login might be dropped
  11637.     -- outside replication already.
  11638.  
  11639.     select @database = db_name()
  11640.  
  11641.     EXEC @retcode = dbo.sp_helpdistributor  @rpcsrvname = @dist_rpcname OUTPUT,
  11642.                                         @distribdb   = @distribdb   OUTPUT
  11643.     IF @@error <> 0
  11644.     BEGIN
  11645.          RAISERROR (14071, 16, -1)
  11646.          return (1)
  11647.     END
  11648.  
  11649.     IF @retcode <> 0 OR @distribdb IS NULL OR @dist_rpcname IS NULL
  11650.     BEGIN
  11651.         RAISERROR (14071, 16, -1)
  11652.         return(1)
  11653.     END
  11654.  
  11655.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSpublication_access'
  11656.  
  11657.     -- Get logins in the PAL if needed
  11658.     if @initial_list = 0
  11659.     begin
  11660.         create table #granted (login sysname collate database_default null)    
  11661.         insert into #granted EXEC @retcode = @distproc
  11662.              @publisher = @@SERVERNAME,
  11663.              @publisher_db = @database,
  11664.              @publication = @publication,
  11665.              @operation = 'help',
  11666.              @login = @login
  11667.  
  11668.         IF @@error <> 0 OR @retcode <> 0
  11669.             return (1)
  11670.     end
  11671.  
  11672.     -- Get distributor valid logins if needed
  11673.     if @return_granted = 0 or @initial_list = 1
  11674.     begin
  11675.         create table #dist_logins(login sysname collate database_default null)
  11676.         insert into #dist_logins EXEC @retcode = @distproc
  11677.              @publisher = @@SERVERNAME,
  11678.              @operation = 'get_logins'
  11679.     end
  11680.  
  11681.     if @initial_list = 1
  11682.     begin
  11683.         -- Get the initial list for the publication to be created
  11684.         -- by the current user
  11685.         -- It contains all the logins sysadmin group and the current user
  11686.         -- that have valid login at the distributor.
  11687.         select l.loginname, l.isntname, l.isntgroup
  11688.          from master..syslogins l, #dist_logins d where
  11689.             l.sid = suser_sid(d.login) and
  11690.             l.hasaccess = 1 and
  11691.             (is_srvrolemember('sysadmin',d.login) = 1 or (l.sid = suser_sid())) 
  11692.     end
  11693.     else if @return_granted = 0
  11694.     begin
  11695.         select l.loginname, l.isntname, l.isntgroup
  11696.          from master..syslogins l, #dist_logins d where
  11697.             l.sid = suser_sid(d.login) and
  11698.             l.hasaccess = 1 and 
  11699.             not exists (select * from #granted g where 
  11700.                 suser_sid(g.login) = l.sid) and
  11701.             exists (select * from sysusers u where
  11702.                 (u.sid = l.sid or u.name = 'guest') and
  11703.                 u.hasdbaccess = 1)
  11704.     end
  11705.     else if @return_granted = 1
  11706.     begin
  11707.         select l.loginname, l.isntname, l.isntgroup from master..syslogins l,
  11708.             #granted g where
  11709.             l.sid = suser_sid(g.login)
  11710.     end
  11711. GO
  11712.  
  11713. raiserror('Creating procedure sp_check_publication_access', 0,1)
  11714. go
  11715. CREATE PROCEDURE sp_check_publication_access
  11716. @publication sysname,
  11717. @given_login sysname = NULL
  11718. AS
  11719.     set nocount on
  11720.     declare @retcode int
  11721.     exec @retcode = dbo.sp_MSreplcheck_pull 
  11722.         @publication = @publication,
  11723.         @given_login = @given_login
  11724.     if @retcode <> 0 or @@error <> 0
  11725.         return (1)
  11726.     return 0
  11727. GO
  11728.  
  11729. exec dbo.sp_MS_marksystemobject sp_check_publication_access
  11730. go
  11731.  
  11732. raiserror('Creating procedure sp_MSget_agent_names', 0,1)
  11733. go
  11734. CREATE PROCEDURE sp_MSget_agent_names
  11735. @publication sysname,        
  11736. @subscriber sysname = NULL,              
  11737. @subscriber_db sysname = NULL,
  11738. @publisher sysname = NULL,
  11739. @publisher_db sysname = NULL
  11740. as
  11741.     set nocount on
  11742.  
  11743.     declare @retcode int
  11744.  
  11745.     if @publisher is null
  11746.     -- We are at the publisher side, call into the distributor
  11747.     begin
  11748.         /*
  11749.         ** Get distribution server information for remote RPC
  11750.         ** agent verification.
  11751.         */
  11752.         declare @distributor sysname
  11753.         declare @distribdb sysname
  11754.         declare @distproc nvarchar(1000), @pubdb sysname
  11755.  
  11756.         select @pubdb = db_name()
  11757.  
  11758.         EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  11759.             @distribdb = @distribdb OUTPUT
  11760.  
  11761.         IF @@error <> 0 OR @retcode <> 0
  11762.         BEGIN
  11763.             RAISERROR (14071, 16, -1)
  11764.             RETURN (1)
  11765.         END
  11766.  
  11767.         /*
  11768.         ** Call proc to change the distributor
  11769.         */
  11770.         SELECT @distproc = RTRIM(@distributor) + '.' + @distribdb + 
  11771.             '.dbo.sp_MSget_agent_names'
  11772.         exec @retcode = @distproc 
  11773.             @publisher = @@SERVERNAME,
  11774.             @publisher_db = @pubdb,
  11775.             @publication = @publication,
  11776.             @subscriber = @subscriber,
  11777.             @subscriber_db = @subscriber_db
  11778.     end
  11779.     else
  11780.     -- We are at the distribution db
  11781.     begin
  11782.         declare @publisher_id smallint
  11783.         select @publisher_id = srvid from master..sysservers where UPPER(srvname) = UPPER(@publisher) collate database_default
  11784.  
  11785.         if @subscriber is null
  11786.         -- Returning publication agents if @subscriber is null
  11787.         begin
  11788.             declare @snapshot_agent sysname, @logreader_agent sysname, @qreader_agent sysname
  11789.             
  11790.             -- Have to use name in sysjobs table because users may rename
  11791.             -- the SQLServerAgent jobs. If users deleted the jobs, DMO scripting will
  11792.             -- create new jobs.
  11793.  
  11794.             select 
  11795.                 'snapshot_agent' = (select j.name from MSsnapshot_agents a, msdb..sysjobs j where
  11796.                     publisher_id = @publisher_id and
  11797.                     publisher_db = @publisher_db collate database_default and
  11798.                     publication = @publication collate database_default and
  11799.                     a.job_id = j.job_id),
  11800.                 'logreader_agent' = (select j.name from MSlogreader_agents a, msdb..sysjobs j where
  11801.                     publisher_id = @publisher_id and
  11802.                     publisher_db = @publisher_db collate database_default and
  11803.                     a.job_id = j.job_id),
  11804.                 'qreader_agent' = (select j.name from MSqreader_agents a, msdb..sysjobs j where
  11805.                     a.job_id = j.job_id)
  11806.         end
  11807.         else
  11808.         begin
  11809.             declare @subscriber_id smallint
  11810.             select @subscriber_id = srvid from master..sysservers where UPPER(srvname) = UPPER(@subscriber) collate database_default
  11811.             declare @publication_type int, @independent_agent bit
  11812.  
  11813.             select @publication_type = publication_type, @independent_agent = independent_agent
  11814.                 from MSpublications where 
  11815.                     publisher_id = @publisher_id and
  11816.                     publisher_db = @publisher_db and
  11817.                     publication = @publication
  11818.  
  11819.             if @publication_type = 2
  11820.             -- Merge publication, get merge agent name
  11821.             begin
  11822.                 select 'merge_agent' = j.name from MSmerge_agents a, msdb..sysjobs j where
  11823.                     publisher_id = @publisher_id and
  11824.                     publisher_db = @publisher_db collate database_default and
  11825.                     publication = @publication collate database_default and
  11826.                     subscriber_id = @subscriber_id and
  11827.                     subscriber_db = @subscriber_db collate database_default and
  11828.                     a.job_id = j.job_id
  11829.             end
  11830.             else
  11831.             begin
  11832.                 select 'distribution_agent' = j.name from MSdistribution_agents a, msdb..sysjobs j where
  11833.                     publisher_id = @publisher_id and
  11834.                     publisher_db = @publisher_db collate database_default and
  11835.                     (publication = @publication collate database_default or
  11836.                      publication = N'ALL' and @independent_agent = 0) and
  11837.                     subscriber_id = @subscriber_id and
  11838.                     subscriber_db = @subscriber_db collate database_default and
  11839.                     a.job_id = j.job_id
  11840.             end
  11841.         end
  11842.     end -- If @publisher is null
  11843.  
  11844. go
  11845.  
  11846. exec dbo.sp_MS_marksystemobject sp_MSget_agent_names
  11847. go
  11848.  
  11849. raiserror('Creating procedure sp_MSinit_replication_perfmon', 0,1)
  11850. go
  11851.  
  11852. create proc sp_MSinit_replication_perfmon
  11853. as
  11854.     declare @agent_type int
  11855.     declare @agent_name nvarchar(100)
  11856.     declare @status int
  11857.  
  11858.     -- Remove all existing instances
  11859.     dbcc deleteinstance ("SQL Replication Agents", "%")
  11860.     dbcc deleteinstance ("SQL Replication Snapshot", "%")
  11861.     dbcc deleteinstance ("SQL Replication Logreader", "%")
  11862.     dbcc deleteinstance ("SQL Replication Distribution", "%")
  11863.     dbcc deleteinstance ("SQL Replication Merge", "%")
  11864.     dbcc deleteinstance ("SQL Replication Queuereader", "%")
  11865.  
  11866.     -- Add and initialize Perfmon SQL Replication Agents instances
  11867.     dbcc addinstance ("SQL Replication Agents", "Snapshot")
  11868.     dbcc addinstance ("SQL Replication Agents", "Logreader")
  11869.     dbcc addinstance ("SQL Replication Agents", "Distribution")
  11870.     dbcc addinstance ("SQL Replication Agents", "Merge")
  11871.     dbcc addinstance ("SQL Replication Agents", "Queuereader")
  11872.     dbcc setinstance ("SQL Replication Agents", "Running", "Snapshot", 0)
  11873.     dbcc setinstance ("SQL Replication Agents", "Running", "Logreader", 0)
  11874.     dbcc setinstance ("SQL Replication Agents", "Running", "Distribution", 0)
  11875.     dbcc setinstance ("SQL Replication Agents", "Running", "Merge", 0)
  11876.     dbcc setinstance ("SQL Replication Agents", "Running", "Queuereader", 0)
  11877.  
  11878.     -- Add instances for each agent currently in the status table
  11879.     declare hC CURSOR LOCAL FAST_FORWARD for select agent_type, agent_name, status from tempdb.dbo.MSreplication_agent_status for read only
  11880.     open hC
  11881.     fetch hC into @agent_type, @agent_name, @status
  11882.     while (@@fetch_status <> -1)
  11883.     begin
  11884.  
  11885.         if @agent_type = 1 
  11886.         begin
  11887.             dbcc addinstance ("SQL Replication Snapshot", @agent_name)
  11888.             if @status = 1 or @status = 3 or @status = 4
  11889.                 dbcc incrementinstance ("SQL Replication Agents", "Running", "Snapshot", 1)
  11890.         end
  11891.         else if @agent_type = 2 
  11892.         begin
  11893.             dbcc addinstance ("SQL Replication Logreader", @agent_name)
  11894.             if @status = 1 or @status = 3 or @status = 4
  11895.                 dbcc incrementinstance ("SQL Replication Agents", "Running", "Logreader", 1)
  11896.         end
  11897.         else if @agent_type = 3 
  11898.         begin
  11899.             dbcc addinstance ("SQL Replication Distribution", @agent_name)
  11900.             if @status = 1 or @status = 3 or @status = 4
  11901.                 dbcc incrementinstance ("SQL Replication Agents", "Running", "Distribution", 1)
  11902.         end
  11903.         else if @agent_type = 4 
  11904.         begin
  11905.             dbcc addinstance ("SQL Replication Merge", @agent_name)
  11906.             if @status = 1 or @status = 3 or @status = 4
  11907.                 dbcc incrementinstance ("SQL Replication Agents", "Running", "Merge", 1)
  11908.         end
  11909.         else if @agent_type = 9 
  11910.         begin
  11911.             dbcc addinstance ("SQL Replication Queuereader", @agent_name)
  11912.             if @status = 1 or @status = 3 or @status = 4
  11913.                 dbcc incrementinstance ("SQL Replication Agents", "Running", "Queuereader", 1)
  11914.         end
  11915.  
  11916.         fetch hC into @agent_type, @agent_name, @status
  11917.     end
  11918.     close hC
  11919.     deallocate hC
  11920. GO
  11921.  
  11922. raiserror('Creating procedure sp_MSrepl_startup', 0,1)
  11923. go
  11924. create procedure sp_MSrepl_startup
  11925. as
  11926.  
  11927.     -- Drop and create publisher side cache table
  11928.     if exists (select * from tempdb..sysobjects where name = 'MSpublisher_access' and
  11929.     type = 'U')
  11930.         drop table tempdb.dbo.MSpublisher_access
  11931.  
  11932.     create table tempdb.dbo.MSpublisher_access
  11933.     (
  11934.     spid int NOT NULL,
  11935.     db_id int not null,
  11936.     publication sysname not null,
  11937.     login_time datetime not null,
  11938.     pubid uniqueidentifier null -- Used for merge only.
  11939.     )
  11940.  
  11941.     CREATE CLUSTERED INDEX ucMSpublisher_access ON tempdb.dbo.MSpublisher_access
  11942.         (spid, publication, db_id)
  11943.  
  11944.     exec tempdb.dbo.sp_MS_marksystemobject MSpublisher_access
  11945.     -- Drop and create distributor side cache table
  11946.     -- We need to avoid publisher and distributor using the same table to prevent
  11947.     -- contention in local distributor case.
  11948.     if exists (select * from master..sysservers
  11949.                   WHERE  srvstatus & 8 <> 0 and UPPER(datasource) = UPPER(@@servername) collate database_default)
  11950.     begin
  11951.         if exists (select * from tempdb..sysobjects where name = 'MSdistributor_access' and
  11952.         type = 'U')
  11953.             drop table tempdb.dbo.MSdistributor_access
  11954.  
  11955.         create table tempdb.dbo.MSdistributor_access
  11956.         (
  11957.         spid int NOT NULL,
  11958.         db_id int not null,
  11959.         agent_id int not null,
  11960.         agent_type int not null,
  11961.         publication_id int not null,
  11962.         login_time datetime not null
  11963.         )
  11964.  
  11965.         CREATE CLUSTERED INDEX ucMSdistributor_access ON tempdb.dbo.MSdistributor_access
  11966.             (spid, agent_id, db_id)
  11967.         exec tempdb.dbo.sp_MS_marksystemobject MSdistributor_access
  11968.         -- Create repl monitor table
  11969.         exec sp_MScreate_replication_status_table
  11970.    end
  11971. go
  11972.  
  11973. -- If a distributor is installed, mark the sp as a startup sp. 
  11974. if exists (select * FROM master..sysservers WHERE  srvstatus & 8 <> 0)
  11975.     exec dbo.sp_procoption 'sp_MSrepl_startup', 'startup', 'true' 
  11976. go
  11977.  
  11978. raiserror('Creating procedure sp_MSflush_access_cache', 0,1)
  11979. go
  11980. CREATE PROCEDURE sp_MSflush_access_cache 
  11981. AS
  11982.     -- Delete all the 'dead' connections in MSpublisher_access.
  11983.     delete tempdb.dbo.MSpublisher_access 
  11984.         from tempdb.dbo.MSpublisher_access a
  11985.         where not exists (select * from master..sysprocesses p where
  11986.             a.spid = p.spid and
  11987.             p.login_time = a.login_time)
  11988.     if @@error <> 0
  11989.         return 1
  11990.     else
  11991.         return 0
  11992.  
  11993.     if exists (select * from tempdb..sysobjects where name = 'MSdistributor_access' and
  11994.     type = 'U')
  11995.     begin
  11996.         -- Delete all the 'dead' connections in MSdistributor_access.
  11997.         delete tempdb.dbo.MSdistributor_access 
  11998.             from tempdb.dbo.MSdistributor_access a
  11999.             where not exists (select * from master..sysprocesses p where
  12000.                 a.spid = p.spid and
  12001.                 p.login_time = a.login_time)
  12002.         if @@error <> 0
  12003.             return 1
  12004.         else
  12005.             return 0
  12006.     end
  12007.     return (0)
  12008. GO
  12009.  
  12010. raiserror('Creating procedure sp_MSreinit_failed_subscriptions', 0,1)
  12011. go
  12012. -- This stored procedure is used as a response to the Replication Validation Failure Alert.
  12013. -- It will reinit the failed subscription. If the publisher is remote, it must be configured as a remote server 
  12014. -- for this procedure to work.
  12015. create proc sp_MSreinit_failed_subscriptions
  12016. @failure_level int = 0      -- 0 All failure  1 Validation failures
  12017. as
  12018.  
  12019.     declare @publisher sysname
  12020.     declare @publisher_db sysname
  12021.     declare @publication sysname
  12022.     declare @article sysname
  12023.     declare @publication_type int
  12024.     declare @subscriber sysname
  12025.     declare @subscriber_db sysname
  12026.     declare @agent_type int
  12027.     declare @alert_id int
  12028.     declare @proc nvarchar(100)
  12029.     declare @message nvarchar(4000)
  12030.     declare @retcode int
  12031.     declare @found bit
  12032.     declare @return_value int
  12033.     
  12034.     set nocount on
  12035.  
  12036.     set @found = 0          -- set if cursor returns a row
  12037.     set @return_value = 0   -- set to success
  12038.  
  12039.     -- For each publication validation failure, resync the subscription
  12040.     declare hc CURSOR LOCAL FAST_FORWARD for select publisher, publisher_db, publication, publication_type, article, subscriber, 
  12041.         subscriber_db, alert_id from 
  12042.         msdb..sysreplicationalerts where
  12043.         (@failure_level = 0 or (@failure_level = 1 and alert_error_code = 20574)) and   -- 20574 = validation failure
  12044.         status = 0
  12045.         for read only
  12046.  
  12047.     open hc
  12048.     fetch hc into  @publisher, @publisher_db, @publication, @publication_type, @article, @subscriber, @subscriber_db, @alert_id
  12049.     while (@@fetch_status <> -1)
  12050.     begin
  12051.         
  12052.         set @found = 1
  12053.  
  12054.         -- Reinit snapshot or transactional subscription (article level)
  12055.         if @publication_type = 0 or @publication_type = 1
  12056.         begin
  12057.             set @proc = @publisher + '.' + @publisher_db + '.dbo.sp_reinitsubscription'
  12058.             exec @retcode = @proc
  12059.                 @publication = @publication,
  12060.                 @article = @article,
  12061.                 @subscriber = @subscriber,
  12062.                 @destination_db = @subscriber_db
  12063.             -- Ignore failures, update status bit if successful
  12064.             if @retcode = 0
  12065.             begin
  12066.                 -- Change status to 1 which means the alerts has been serviced
  12067.                 update msdb..sysreplicationalerts set status = 1 where alert_id = @alert_id
  12068.  
  12069.                 -- Raiserror that subscription was reinitialized
  12070.                 if @failure_level = 0
  12071.                     -- 'Subscriber ''%s'' subscription to article ''%s'' in publication ''%s'' has been reinitialized after a synchronization failure.'
  12072.                     raiserror(20576, 10,-1, @subscriber, @article, @publication) 
  12073.                 else if @failure_level  = 1
  12074.                     -- 'Subscriber ''%s'' subscription to article ''%s'' in publication ''%s'' has been reinitialized after a validation failure.'
  12075.                     raiserror(20572, 10,-1, @subscriber, @article, @publication) 
  12076.             end
  12077.             else -- failure
  12078.                 set @return_value = 1
  12079.         end
  12080.         -- Reinit merge subscription (full publication)
  12081.         else if @publication_type = 2       
  12082.         begin
  12083.             set @proc = @publisher + '.' + @publisher_db + '.dbo.sp_reinitmergesubscription'
  12084.             exec @retcode = @proc
  12085.                 @publication = @publication,
  12086.                 @subscriber = @subscriber,
  12087.                 @subscriber_db = @subscriber_db
  12088.             -- Ignore failures, update status bit if successful
  12089.             if @retcode = 0
  12090.             begin
  12091.                 -- Change status to 1 which means the alerts has been serviced
  12092.                 update msdb..sysreplicationalerts set status = 1 where alert_id = @alert_id
  12093.  
  12094.                 -- Raiserror that subscription was reinitialized
  12095.                 if @failure_level = 0
  12096.                     -- 'Subscriber ''%s'' subscription to to article ''%s'' in publication ''%s'' has been reinitialized after a synchronization failure.'
  12097.                      raiserror(20576, 10,-1, @subscriber, @article, @publication) 
  12098.                 else if @failure_level  = 1
  12099.                     -- 'Subscriber ''%s'' subscription to to article ''%s'' in publication ''%s'' has been reinitialized after a validation failure.'
  12100.                     raiserror(20572, 10,-1, @subscriber, @article, @publication)
  12101.             end
  12102.             else -- failure
  12103.                 set @return_value = 1
  12104.         end
  12105.  
  12106.         fetch hc into  @publisher, @publisher_db, @publication, @publication_type, @article, @subscriber, @subscriber_db, @alert_id
  12107.     end
  12108.  
  12109.     close hc
  12110.     deallocate hc
  12111.  
  12112.     -- Return a message stating no entries where found
  12113.     if @found = 0
  12114.     begin
  12115.         -- 'No entries were found in msdb..sysreplicationalerts.'
  12116.         raiserror(20577, 10,-1) 
  12117.  
  12118.         -- There is most likely a problem, set failure return value
  12119.         set @return_value = 1
  12120.     end
  12121.  
  12122.     return @return_value 
  12123. go
  12124. dump tran master with no_log
  12125. GO
  12126.  
  12127. raiserror('Creating procedure sp_add_datatype_mapping', 0,1)
  12128. go
  12129.  
  12130. -- Add a row into the "MSdatatype_mappings" table
  12131. create procedure sp_add_datatype_mapping (
  12132.     @dbms_name          sysname,
  12133.     @sql_type           sysname,
  12134.     @dest_type          sysname,
  12135.     @dest_prec          int,
  12136.     @dest_create_params int,
  12137.     @dest_nullable      bit
  12138. )
  12139. as
  12140.     set nocount on
  12141.  
  12142.     if @dbms_name is null
  12143.     BEGIN
  12144.         RAISERROR (14043, 16, -1, '@dbms_name')
  12145.         RETURN (1)
  12146.     END
  12147.     
  12148.     if @sql_type is null 
  12149.     BEGIN
  12150.         RAISERROR (14043, 16, -1, '@sql_type')
  12151.         RETURN (1)
  12152.     END
  12153.  
  12154.     if @dest_type is null
  12155.     BEGIN
  12156.         RAISERROR (14043, 16, -1, '@dest_type')
  12157.         RETURN (1)
  12158.     END
  12159.  
  12160.     if @dest_prec is null
  12161.     BEGIN
  12162.         RAISERROR (14043, 16, -1, '@dest_prec')
  12163.         RETURN (1)
  12164.     END
  12165.  
  12166.     if @dest_create_params is null
  12167.     BEGIN
  12168.         RAISERROR (14043, 16, -1, '@dest_create_params')
  12169.         RETURN (1)
  12170.     END
  12171.  
  12172.     if @dest_nullable is null
  12173.     BEGIN
  12174.         RAISERROR (14043, 16, -1, '@dest_nullable')
  12175.         RETURN (1)
  12176.     END
  12177.  
  12178.  
  12179.     insert into msdb..MSdatatype_mappings
  12180.     values (@dbms_name, @sql_type, @dest_type, @dest_prec, @dest_create_params, @dest_nullable)
  12181.  
  12182.     if @@error <> 0
  12183.         return(1)
  12184. GO
  12185.  
  12186. raiserror('Creating procedure sp_MSrepl_gettype_mappings', 0,1)
  12187. go
  12188. create procedure sp_MSrepl_gettype_mappings(
  12189.     @dbms_name          sysname,
  12190.     @sql_type           sysname = '%',
  12191.     @source_prec        int = NULL
  12192. )
  12193. as
  12194.     set nocount on
  12195.     declare @retcode int
  12196.     declare @distributor sysname
  12197.     declare @distribdb sysname
  12198.     declare @distproc nvarchar (255)
  12199.     
  12200.     EXECUTE @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  12201.            @distribdb   = @distribdb OUTPUT
  12202.     IF @@ERROR <> 0 or @retcode <> 0
  12203.     BEGIN
  12204.         RAISERROR (14071, 16, -1)
  12205.         RETURN (1)
  12206.     END
  12207.  
  12208.     SELECT @distproc = RTRIM(@distributor) + '.master' + 
  12209.             '.dbo.sp_help_datatype_mapping'
  12210.  
  12211.     EXECUTE @retcode = @distproc
  12212.                        @dbms_name = @dbms_name,
  12213.                        @sql_type = @sql_type,
  12214.                        @source_prec = @source_prec
  12215.             
  12216.     IF @@ERROR <> 0 OR @retcode <> 0
  12217.         return 1
  12218.     ELSE
  12219.         return 0
  12220. go
  12221.  
  12222. raiserror('Creating procedure sp_help_datatype_mapping', 0,1)
  12223. go
  12224. create procedure sp_help_datatype_mapping(
  12225.     @dbms_name          sysname,
  12226.     @sql_type           sysname = '%',
  12227.     @source_prec        int = NULL
  12228. )
  12229. as
  12230.     set nocount on
  12231.     
  12232.     if @source_prec is NULL
  12233.     begin
  12234.         select sql_type, dest_type, dest_prec, dest_create_params, dest_nullable
  12235.         from msdb..MSdatatype_mappings 
  12236.         where dbms_name = @dbms_name collate database_default
  12237.         and sql_type like @sql_type collate database_default
  12238.     end
  12239.     else
  12240.     begin
  12241.         select sql_type, dest_type, dest_prec, dest_create_params, dest_nullable
  12242.         from msdb..MSdatatype_mappings 
  12243.         where dbms_name = @dbms_name collate database_default
  12244.         and sql_type like @sql_type collate database_default
  12245.         and dest_prec = (select min(dest_prec)
  12246.                             from msdb..MSdatatype_mappings 
  12247.                             where dbms_name = @dbms_name collate database_default
  12248.                             and sql_type like @sql_type collate database_default
  12249.                             and dest_prec >= @source_prec
  12250.                         )
  12251.     end
  12252. GO
  12253.  
  12254. raiserror('Creating procedure sp_MSfix_6x_tasks', 0,1)
  12255. go
  12256. CREATE PROCEDURE sp_MSfix_6x_tasks (
  12257.     @publisher sysname = NULL
  12258. ) AS
  12259.  
  12260.     SET NOCOUNT ON
  12261.  
  12262.     DECLARE @distributor sysname
  12263.     DECLARE @distproc nvarchar (255)
  12264.     declare @retcode int
  12265.  
  12266.     -- If @publisher is null redirect the call to distributor
  12267.     if @publisher is null
  12268.     begin
  12269.         /*
  12270.         ** Get distribution server information for remote RPC
  12271.         ** agent verification.
  12272.         */
  12273.         EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT
  12274.  
  12275.         IF @@error <> 0 OR @retcode <> 0
  12276.         BEGIN
  12277.             RAISERROR (14071, 16, -1)
  12278.             RETURN (1)
  12279.         END
  12280.  
  12281.         /*
  12282.         ** RPC distributor
  12283.         */
  12284.         SELECT @distproc = RTRIM(@distributor) + '.master.dbo.sp_MSfix_6x_tasks'
  12285.  
  12286.         EXECUTE @retcode = @distproc
  12287.             @publisher = @@SERVERNAME
  12288.         IF @@ERROR <> 0 or @retcode <> 0
  12289.             return (1)
  12290.     end
  12291.     else
  12292.     begin
  12293.  
  12294.         declare @category_id int
  12295.         declare @category_name sysname
  12296.         declare @server sysname
  12297.         declare @databasename sysname
  12298.         declare @name sysname
  12299.         declare @distdb sysname
  12300.         declare @job_id uniqueidentifier
  12301.         declare @sSubsystem sysname
  12302.  
  12303.         -- Drop entry in systasks first.
  12304.         DECLARE hCtasks CURSOR LOCAL FAST_FORWARD FOR
  12305.             SELECT name FROM msdb.dbo.systasks_view st WHERE 
  12306.                 
  12307.                 -- drop distribution agents.
  12308.                 (st.name LIKE @publisher + '_' + '%' + '_' + '%' AND 
  12309.                 LOWER(subsystem collate SQL_Latin1_General_CP1_CS_AS) = 'distribution' AND
  12310.                 server = @@SERVERNAME) OR
  12311.                                 
  12312.                 (LOWER(subsystem collate SQL_Latin1_General_CP1_CS_AS) = 'logreader' AND
  12313.                 server = @publisher) OR 
  12314.                 
  12315.                 (LOWER(subsystem collate SQL_Latin1_General_CP1_CS_AS) = 'snapshot' AND
  12316.                 server = @publisher) OR
  12317.  
  12318.                 (st.name LIKE '%' + '_' + '%' + '_Cleanup' AND 
  12319.                 st.command LIKE '%' + 'sp_replcleanup' + '%' AND
  12320.                 LOWER(subsystem collate SQL_Latin1_General_CP1_CS_AS) = 'tsql')
  12321.             FOR READ ONLY
  12322.  
  12323.         OPEN hCtasks
  12324.         FETCH hCtasks INTO @name
  12325.  
  12326.         WHILE (@@fetch_status <> -1)
  12327.         BEGIN
  12328.             
  12329.             EXEC @retcode = msdb.dbo.sp_droptask @name = @name
  12330.             if @retcode <> 0 or @@error <> 0
  12331.                 return(1)
  12332.             FETCH hCtasks INTO @name
  12333.         END
  12334.  
  12335.         -- Now spin through each old replication job and fixup categories names
  12336.         declare hcJobsToFix CURSOR LOCAL FAST_FORWARD for 
  12337.                 select distinct j.job_id, j.name, s.subsystem, 
  12338.                     s.server, s.database_name
  12339.                     from msdb.dbo.sysjobs j left join msdb.dbo.sysjobsteps s
  12340.                         on j.job_id = s.job_id
  12341.                     where j.category_id = 0 and s.step_id = 1 and lower(s.subsystem collate SQL_Latin1_General_CP1_CS_AS) in ( 'snapshot', 'logreader', 'distribution' )
  12342.                 for read only
  12343.  
  12344.         open hcJobsToFix 
  12345.         fetch hcJobsToFix into @job_id, @name, @sSubsystem,
  12346.             @server,@databasename
  12347.  
  12348.         while (@@fetch_status <> -1 )
  12349.         begin
  12350.             -- Note, have to make it a transaction, once the category_id is changed,
  12351.             -- the task will never be picked up again.
  12352.             begin tran 
  12353.  
  12354.                 -- Get Distribution category name (assumes category_id = 10)
  12355.                 select @category_id =
  12356.                     case lower(@sSubsystem collate SQL_Latin1_General_CP1_CS_AS) 
  12357.                         when 'snapshot' then 15
  12358.                         when 'distribution' then 10
  12359.                         when 'logreader' then 13
  12360.                         else 0
  12361.                     end
  12362.  
  12363.                 select @category_name = name FROM msdb.dbo.syscategories where category_id = @category_id
  12364.                 
  12365.                 -- raiserror( 'Would update %s to category %d based on subsystem value %s', -1, 10, @nJobName, @iCategory, @sSubsystem )
  12366.                 exec @retcode = msdb.dbo.sp_update_job @job_id = @job_id, 
  12367.                     @category_name = @category_name
  12368.                 if @retcode <> 0 or @@error <> 0
  12369.                     goto UNDO
  12370.  
  12371.  
  12372.                 -- Add the replication agent for monitoring
  12373.                 SELECT @distdb = distribution_db from msdb..MSdistpublishers where UPPER(name) = UPPER(@server) collate database_default
  12374.                 IF (@category_id = 13) -- Logreader
  12375.                 BEGIN
  12376.                     SELECT @distproc = @distdb + '.dbo.sp_MSadd_logreader_agent'
  12377.                     EXECUTE @retcode = @distproc
  12378.                         @name = @name,
  12379.                         @publisher = @server,
  12380.                         @publisher_db = @databasename,
  12381.                         @publication = '',  
  12382.                         @local_job = 1,
  12383.                         @job_existing = 1,
  12384.                         @job_id = @job_id
  12385.  
  12386.                     IF (@retcode <> 0 or @@error<>0)
  12387.                         goto UNDO
  12388.                 END
  12389.                 ELSE 
  12390.                 IF (@category_id = 15) -- Snapshot
  12391.                 BEGIN
  12392.                     DECLARE @publication sysname
  12393.  
  12394.                     SELECT @publication = NULL
  12395.                     EXECUTE master.dbo.sp_MSget_publication_from_taskname
  12396.                                             @taskname = @name,
  12397.                                             @publisher = @server,
  12398.                                             @publisherdb = @databasename,
  12399.                                             @publication = @publication OUTPUT
  12400.  
  12401.                     IF (@publication IS NOT NULL )
  12402.                     BEGIN                
  12403.                         SELECT @distproc = @distdb + '.dbo.sp_MSadd_publication'
  12404.                         EXECUTE @retcode = @distproc
  12405.                             @publisher = @server,
  12406.                             @publisher_db = @databasename,
  12407.                             @publication = @publication,
  12408.                             @publication_type = 0 -- Transactional
  12409.                         IF (@retcode <> 0 or @@error<>0)
  12410.                             goto UNDO
  12411.  
  12412.                         SELECT @distproc = @distdb + '.dbo.sp_MSadd_snapshot_agent'
  12413.                         EXECUTE @retcode = @distproc
  12414.                             @name = @name,
  12415.                             @publisher = @server,
  12416.                             @publisher_db = @databasename,
  12417.                             @publication = @publication,
  12418.                             @local_job = 1,
  12419.                             @job_existing = 1,
  12420.                             @snapshot_jobid = @job_id
  12421.                         IF (@retcode <> 0 or @@error<>0)
  12422.                             goto UNDO
  12423.                     END
  12424.                 END
  12425.             commit tran
  12426.             fetch hcJobsToFix into @job_id, @name, @sSubsystem,
  12427.                 @server,@databasename
  12428.         end            
  12429.         close hcJobsToFix
  12430.         deallocate hcJobsToFix
  12431.     end
  12432.     return (0)
  12433. UNDO:
  12434.     if @@trancount <> 0
  12435.         rollback tran
  12436.     return (1)
  12437. GO
  12438.  
  12439. /*
  12440.  * Name :       sp_MShelpconflictpublications
  12441.  * Description: This sp returns a list of publications or subscriptions in the current
  12442.  *    database that may have conflicts. Results are ordered by publication
  12443.  *    name.
  12444.  * Parameters:  1. publication_type( sysname; '%'==ALL (default) | 'merge' | 'queued' )
  12445.  * Output Result Set has the following structure:
  12446.  *  ----------------------------------------------------------------------------------
  12447.  *      Name                Datatype                Description
  12448.  *  ----------------------------------------------------------------------------------
  12449.  *  a. name                    (sysname)            Publication name
  12450.  *  b. publication_type        (varchar(9))        'merge' | 'queued' | 'immediate' (reserved)
  12451.  *  c. merge_pub_id            (uniqueidentifier)    Merge publication identifier
  12452.  *  d. queued_pub_id        (integer)            Queued publication identifier (I would prefer to name this as tran_pub_id)
  12453.  *  e. sub_agent_id            (integer)            Unique publication identifier on a tran subscriber
  12454.  *  NOTE: In case of queued tran publications, either d or e will have a value at any time
  12455.  *  and this will also indicate, if we are processing a subscriber of a publication (d 
  12456.  *  will be NULL and e will have a value) or if we are processing the publisher side (d
  12457.  *  will have a value and e will be NULL)
  12458. */
  12459. raiserror('Creating procedure sp_MShelpconflictpublications', 0,1)
  12460. GO
  12461.  
  12462. CREATE PROCEDURE sp_MShelpconflictpublications ( @publication_type varchar(9) ='%' )
  12463. AS 
  12464. BEGIN
  12465.  
  12466.     SET nocount ON
  12467.  
  12468.         DECLARE @retcode int
  12469.  
  12470.         /*
  12471.         **  Security check.  restrict to 'sysadmin' and member of db_owner role
  12472.         */ 
  12473.         exec @retcode = dbo.sp_MSreplcheck_publish
  12474.         if @@ERROR <> 0 or @retcode <> 0
  12475.             return (1)
  12476.     
  12477.     -- validate publication type
  12478.     IF NOT( lower(@publication_type collate SQL_Latin1_General_CP1_CS_AS) IN ('%', 'merge', 'queued') )
  12479.     BEGIN
  12480.         raiserror( 'invalid publication type', 16, -1 )
  12481.         RETURN (1)
  12482.     END
  12483.     
  12484.     -- temp table to store combined result set
  12485.     CREATE TABLE #result_list ( publication sysname collate database_default, publication_type VARCHAR(9) DEFAULT 'merge', 
  12486.                                 merge_pub_id UNIQUEIDENTIFIER NULL, 
  12487.                                 queued_pub_id INTEGER NULL, sub_agent_id INTEGER NULL, publisher sysname collate database_default NULL, publisher_db sysname collate database_default NULL)
  12488.  
  12489.     -- fetch merge results into temp table; need not affect any rows
  12490.     IF ( lower(@publication_type collate SQL_Latin1_General_CP1_CS_AS) IN ('%', 'merge') )
  12491.     BEGIN
  12492.         IF EXISTS( SELECT * FROM sysobjects WHERE name = 'sysmergepublications' )
  12493.             INSERT #result_list ( publication, merge_pub_id, publisher, publisher_db)
  12494.                 EXEC sp_MShelpmergeconflictpublications
  12495.  
  12496.         -- may return 18757 (not merge published) and that is ok if @publication_type = ALL
  12497.         IF ( @@ERROR <> 0) AND ( @@ERROR = 18757 AND @publication_type = '%' ) 
  12498.             GOTO FAILURE
  12499.                 
  12500.     END
  12501.  
  12502.     -- fetch tran results into temp table
  12503.     IF ( lower(@publication_type collate SQL_Latin1_General_CP1_CS_AS) IN ('%', 'queued') )
  12504.     BEGIN
  12505.         INSERT #result_list  ( publication, publication_type, merge_pub_id , queued_pub_id, sub_agent_id )
  12506.             EXEC sp_MShelptranconflictpublications
  12507.         IF ( @@ERROR <> 0) 
  12508.             GOTO FAILURE
  12509.  
  12510.     END
  12511.  
  12512.     -- return combined result list
  12513.     SELECT * FROM #result_list
  12514.  
  12515.     IF EXISTS( SELECT * FROM sysobjects WHERE type = 'U' AND name = '#result_list' )
  12516.         DROP TABLE #result_list
  12517.  
  12518.     RETURN (0)
  12519.  
  12520. FAILURE:
  12521.  
  12522.     IF EXISTS( SELECT * FROM sysobjects WHERE type = 'U' AND name = '#result_list' )
  12523.         DROP TABLE #result_list
  12524.  
  12525.     RETURN (1)
  12526.     
  12527. END
  12528. GO
  12529.  
  12530. raiserror('Creating function system_function_schema.fn_replgetagentcommandlinefromjobid',0,1)
  12531. go
  12532. --
  12533. -- Name: fn_replgetagentcommandlinefromjobid
  12534. --
  12535. -- Description: This function retrieves the command line  
  12536. --              of the replication agent with the 
  12537. --              specified job_id.
  12538. --
  12539. -- Parameters: @agenttype nvarchar(16)
  12540. --             @job_id uniqueidentifier
  12541. -- 
  12542. -- Returns: nvarchar(3200) 
  12543. --
  12544. -- Notes: This function can return null if the 
  12545. --        the given job_id doesn't belong to a
  12546. --        replication agent of the specified type.
  12547. --
  12548. -- Security: Admin access only
  12549. -- 
  12550. create function system_function_schema.fn_replgetagentcommandlinefromjobid (
  12551.     @agenttype nvarchar(16),
  12552.     @job_id uniqueidentifier
  12553.     ) returns nvarchar(3200)
  12554. as
  12555. begin
  12556.     declare @commandline nvarchar(3200)
  12557.     select @commandline = null
  12558.     select @agenttype = upper(@agenttype collate SQL_Latin1_General_CP1_CS_AS)
  12559.     select @commandline = command 
  12560.       from msdb.dbo.sysjobsteps
  12561.      where job_id = @job_id
  12562.        and upper(subsystem collate SQL_Latin1_General_CP1_CS_AS) = @agenttype collate SQL_Latin1_General_CP1_CS_AS
  12563.  
  12564.     return @commandline
  12565. end
  12566. go
  12567.  
  12568. raiserror('Creating procedure sp_replproberemoteserver', 0,1)
  12569. go
  12570. --
  12571. -- Name: sp_replproberemoteserver
  12572. --
  12573. -- Description: This is a lightweight wrapper for calling xp_replproberremsrv
  12574. --              with the added nicety of looking up the agent command line
  12575. --              using the given jobid. This procedure supports two different
  12576. --              modes of operation based on the @no_rpc parameter. If the 
  12577. --              @no_rpc parameter is 1, this procedure will not attempt to make
  12578. --              rpc call to the Distributor. The local mode is mainly useful 
  12579. --              for verifying remote pull subscription agent.
  12580. -- 
  12581. -- Parameters: @remoteservername sysname (mandatory) 
  12582. --             @agent_type nvarchar(16) (optional, default = null) 
  12583. --             @agent_jobid uniqueidentifier (optional default = null)
  12584. --             @no_rpc bit (optional default = 0)
  12585. --
  12586. -- Notes: If @job_id is null, only activation verification will be carried
  12587. --        out.
  12588. --
  12589. -- Security: Execute permission of this procedure is granted to public, 
  12590. --           internal security check is performed to make sure that the
  12591. --           caller is at least a db_owner of the current database.
  12592. --
  12593. -- Result: 'probe_succeeded' 0 or 1
  12594. --
  12595. -- Returns: 0    - succeeded
  12596. --          <> 0 - failed
  12597. --
  12598. create procedure dbo.sp_replproberemoteserver (
  12599.     @remoteservername sysname,
  12600.     @agent_type       nvarchar(16) = null,
  12601.     @agent_jobid      uniqueidentifier = null,
  12602.     @no_rpc           bit = 0
  12603.     ) 
  12604. as
  12605. begin
  12606.     set nocount on
  12607.     declare @retcode        int
  12608.     declare @commandline    nvarchar(3200)
  12609.     declare @distributor    sysname
  12610.     declare @distributiondb sysname     
  12611.     declare @distproc       nvarchar(255)
  12612.     declare @succeeded      nvarchar(10)
  12613.  
  12614.     select @retcode = 0
  12615.     select @commandline = null
  12616.     select @succeeded = null
  12617.  
  12618.     -- security check, db_owner
  12619.     -- Note that this proc can be called from either a publisher database or
  12620.     -- a subscriber database
  12621.     exec @retcode = dbo.sp_MSreplcheck_publish
  12622.     if @@error <> 0 or @retcode <> 0
  12623.         return (1)
  12624.     
  12625.     if @agent_type is null
  12626.     begin
  12627.         select @agent_type = 'distribution'
  12628.     end
  12629.     
  12630.     -- @remoteservername cannot be null or empty
  12631.     select @remoteservername = rtrim(ltrim(@remoteservername))
  12632.     if @remoteservername is null or
  12633.        @remoteservername = N''
  12634.     begin
  12635.         raiserror(21263,16,-1,'@remoteservername')
  12636.         return 1
  12637.     end     
  12638.     
  12639.     -- @agent_type must be 'distribution' or 'merge'
  12640.     if @agent_type not in (N'distribution', N'merge')
  12641.     begin
  12642.         raiserror(21182,16,-1) 
  12643.         return 1
  12644.     end
  12645.  
  12646.     -- Get Distributor information
  12647.     if @no_rpc = 0
  12648.     begin
  12649.         exec @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor output,
  12650.                                           @distribdb = @distributiondb output
  12651.         if @@error <> 0 or @retcode <> 0
  12652.             return 1
  12653.     end
  12654.     else
  12655.     begin
  12656.         select @distributor = @@servername
  12657.     end
  12658.  
  12659.     if upper(@@servername) <> upper(@distributor)
  12660.     begin
  12661.         select @distproc = ltrim(rtrim(@distributor)) + '.' + 
  12662.             'master.dbo.sp_replproberemoteserver'
  12663.         exec @retcode = @distproc @remoteservername, 
  12664.                                   @agent_type, 
  12665.                                   @agent_jobid, 
  12666.                                   1
  12667.         return @retcode
  12668.     end        
  12669.     else
  12670.     begin
  12671.         -- If the given @job_id is not null, try to get
  12672.         -- the agent command line
  12673.         if @agent_jobid is not null 
  12674.         begin
  12675.             select @commandline = fn_replgetagentcommandlinefromjobid(
  12676.                 @agent_type,
  12677.                 @agent_jobid) collate database_default
  12678.  
  12679.             if @commandline is null
  12680.             begin
  12681.                 raiserror(21361,6,-1, @agent_type) 
  12682.                 select 'probe_succeeded' = 0
  12683.                 return 1
  12684.             end
  12685.  
  12686.         end 
  12687.  
  12688.         exec @retcode = master.dbo.xp_replproberemsrv 
  12689.             @remoteservername, 
  12690.             @agent_type, 
  12691.             @succeeded output,
  12692.             @commandline 
  12693.  
  12694.         if lower(@succeeded collate SQL_Latin1_General_CP1_CS_AS) = N'true'  
  12695.             select 'probe_succeeded' = 1
  12696.         else
  12697.             select 'probe_succeeded' = 0
  12698.  
  12699.         return @retcode
  12700.     end
  12701.  
  12702.     select 'probe_succeeded' = 0
  12703.  
  12704.     return 1
  12705. end
  12706. go
  12707. exec sp_MS_marksystemobject sp_replproberemoteserver
  12708. go
  12709. grant execute on dbo.sp_replproberemoteserver to public
  12710. go
  12711.  
  12712. raiserror('Creating procedure sp_MScleanupmergepublisher', 0,1)
  12713. go
  12714. --
  12715. -- Name: sp_MScleanupmergepublisher
  12716. -- 
  12717. -- Description: This procedure currently performs the following function(s):
  12718. --              1) Cleans up all the stale dynamic snapshot views
  12719. --              in all databases enabled for merge replication. This 
  12720. --              procedure should normally be called at merge publisher startup.
  12721. --              
  12722. -- Notes: 1)This procedure is enabled as a startup procedure when a database is
  12723. --        enabled as a first merge publisher database on the server and it 
  12724. --        will be unmarked as a startup procedure when the last merge publisher
  12725. --        database is disabled.
  12726. --        2)Errors within the SP are mostly ignored.
  12727. --        3)This procedure can also be used by admins/securityadmins to perform
  12728. --        manual cleanup of all dynamic snapshot views. Note that cleaning up the 
  12729. --        dynamic snapshot views can disrupt dynamic snapshots that are being generated.
  12730. --        
  12731. -- Returns: (undefined)
  12732. --
  12733. -- Security: Only members of the sysadmin fixed server role can execute this
  12734. --           procedure successfully. So for this procedure to function proprely 
  12735. --           as a startup procedure, the MSSQLServer service account must be a 
  12736. --           member of the sysadmin role.
  12737. --
  12738. create procedure sp_MScleanupmergepublisher
  12739. as
  12740. begin
  12741.     set nocount on
  12742.     declare @status_mask int
  12743.     declare @published_mask int
  12744.     declare @published_database_name sysname
  12745.     declare @command nvarchar(4000)
  12746.  
  12747.     -- Masks off the databases with status that we don't want to deal with
  12748.     select @status_mask = 32 | -- loading
  12749.                           64 | -- pre recovery
  12750.                           128 | -- recovering
  12751.                           256 | -- not recovered
  12752.                           512 | -- offline
  12753.                           1024  -- read only
  12754.  
  12755.     select @published_mask = 4 -- Merge published
  12756.  
  12757.     declare hPublishedDatabase cursor local fast_forward for
  12758.         select name from sysdatabases 
  12759.          where (status & @status_mask) = 0
  12760.            and (category & @published_mask) <> 0
  12761.  
  12762.     open hPublishedDatabase
  12763.  
  12764.     fetch hPublishedDatabase into @published_database_name
  12765.     while  (@@fetch_status <> -1)
  12766.     begin
  12767.  
  12768.         select @command = 'use ' + QUOTENAME(@published_database_name) + '
  12769. ' +                       'exec sp_MScleanupmergepublisherdb'
  12770.  
  12771.  
  12772.         exec (@command)
  12773.         -- Ignore errors
  12774.         fetch hPublishedDatabase into @published_database_name
  12775.     end
  12776.  
  12777.     close hPublishedDatabase
  12778.     deallocate hPublishedDatabase
  12779.  
  12780. end
  12781. go
  12782. exec sp_MS_marksystemobject sp_MScleanupmergepublisher
  12783. go
  12784.  
  12785. --
  12786. -- Name: sp_MScleanupdynsnapshotvws
  12787. --
  12788. -- Description: This procedure is used to drop all the views listed in the     
  12789. --              MSdynamicsnapshotviews table of the publisher database. It is 
  12790. --              typically called by the merge cleanup procedure 
  12791. --              (sp_MScleanupmergepublisherdb) during server startup although
  12792. --              the database administrator can choose to proactively call this
  12793. --              procedure manually to clean up any temporary dynamic snapshot
  12794. --              views left over from an abnormally terminated dynamic snapshot
  12795. --              generation session.
  12796. --
  12797. -- Parameter: (none) 
  12798. --                 
  12799. -- Notes: Calling this procedure while a dynamic snapshot is being generated 
  12800. --        will probably disrupt the said dynamic snapshot generation session.
  12801. -- 
  12802. -- Returns: 0 - succeeded
  12803. --          1 - failed
  12804. -- 
  12805. -- Result: none
  12806. --
  12807. -- Security: Execute permission of this procedure is granted to public. 
  12808. --           Internally, this procedure will call sp_MSreplcheck_publish
  12809. --           to make sure that the caller is either server sysadmin or dbo 
  12810. --           of the publishing database
  12811. --              
  12812. raiserror('Creating procedure sp_MScleanupdynsnapshotvws', 0,1)
  12813. go
  12814. create procedure sp_MScleanupdynsnapshotvws
  12815. as
  12816. begin
  12817.     set nocount on    
  12818.     declare @retcode int
  12819.     select @retcode = 0
  12820.     
  12821.     -- Security check
  12822.     exec @retcode = sp_MSreplcheck_publish
  12823.     if @@error<>0 or @retcode<>0
  12824.     begin
  12825.         return 1
  12826.     end
  12827.  
  12828.     -- Do nothing if the MSdynamicsnapshotviews table does not exist
  12829.     if exists (select * from sysobjects where name = N'MSdynamicsnapshotviews')
  12830.     begin
  12831.         declare @dynamic_snapshot_view_name sysname
  12832.         declare @drop_view_command nvarchar(4000) 
  12833.  
  12834.         declare hViews cursor local fast_forward for
  12835.          select dynamic_snapshot_view_name
  12836.            from MSdynamicsnapshotviews
  12837.         if @@error<>0
  12838.         begin
  12839.             goto Failure
  12840.         end
  12841.         
  12842.         open hViews
  12843.         if @@error<>0
  12844.         begin
  12845.            goto Failure
  12846.         end
  12847.     
  12848.         fetch hViews into @dynamic_snapshot_view_name
  12849.         while (@@fetch_status<>-1)
  12850.         begin
  12851.             select @drop_view_command = 'drop view ' + 
  12852.                 quotename(@dynamic_snapshot_view_name)
  12853.             exec(@drop_view_command)
  12854.             if @@error<>0
  12855.             begin
  12856.                 select @retcode = 1 -- Just to indicate that a failure occurred
  12857.             end        
  12858.             delete from MSdynamicsnapshotviews
  12859.              where dynamic_snapshot_view_name = @dynamic_snapshot_view_name
  12860.             if @@error<>0
  12861.             begin
  12862.                 select @retcode = 1 -- Just to indicate that a failure occurred
  12863.             end        
  12864.             fetch hViews into @dynamic_snapshot_view_name
  12865.         end
  12866.     end
  12867.     return @retcode
  12868. Failure:
  12869.     return 1
  12870. end
  12871. go
  12872. exec sp_MS_marksystemobject 'sp_MScleanupdynsnapshotvws'
  12873. go
  12874. raiserror('Creating procedure sp_MScleanupmergepublisherdb',0,1)
  12875. go
  12876. --
  12877. -- Name: sp_MScleanupmergepublisherdb
  12878. --
  12879. -- Description: This procedure is the per-database analogue of 
  12880. --              sp_MScleanupmergepublisher and it currently performs the 
  12881. --              following function(s):
  12882. --              1) Cleans up all the stale dynamic snapshot views
  12883. --              in the current database.
  12884. --
  12885. -- Returns: (undefined)
  12886. --
  12887. -- Security: Only members of the sysadmin fixed server role can execute this
  12888. --           procedure.
  12889. --
  12890. create procedure sp_MScleanupmergepublisherdb
  12891. as
  12892. begin
  12893.     set nocount on
  12894.     declare @temp_login sysname
  12895.  
  12896.     if exists (select * from sysobjects
  12897.         where name = 'MSdynamicsnapshotviews')
  12898.     begin
  12899.         -- Ignore errors
  12900.         exec sp_MScleanupdynsnapshotvws
  12901.     end
  12902.  
  12903. end
  12904. go
  12905. exec sp_MS_marksystemobject sp_MScleanupmergepublisherdb 
  12906. go
  12907.  
  12908. raiserror('Creating procedure sp_MShelp_replication_table',0,1)
  12909. go
  12910. --
  12911. -- Name: sp_MShelp_replication_table
  12912. --
  12913. -- Description: This procedure is used by DMO to get tables can be published and
  12914. --                their properties
  12915. --
  12916. -- Returns: error code
  12917. --
  12918. -- Security: public
  12919. --
  12920. CREATE PROCEDURE sp_MShelp_replication_table (
  12921.     @table_name sysname = NULL,
  12922.     @table_owner sysname = NULL
  12923.     ) 
  12924. AS
  12925.  
  12926. SET NOCOUNT ON
  12927.  
  12928. declare @objid int
  12929.  
  12930. if @table_name is not null
  12931. begin
  12932.     if @table_owner is NULL
  12933.         select @table_owner = user_name()
  12934.     declare @qualified_table_name    nvarchar(260)
  12935.     select @qualified_table_name = QUOTENAME(@table_owner) + '.' + QUOTENAME(@table_name)
  12936.     select @objid = object_id(@qualified_table_name)
  12937.     if @objid is null
  12938.     begin
  12939.         RAISERROR (14027, 11, -1, @qualified_table_name)
  12940.         return(1)
  12941.     end
  12942. end
  12943.  
  12944. create table #merge_objects (objid int primary key)
  12945.  
  12946. create table #queued_tran_objects (objid int primary key)
  12947.  
  12948. if object_id('sysmergearticles') is not null
  12949.     insert into #merge_objects select distinct objid from sysmergearticles where 
  12950.         objid = @objid or @objid is null
  12951.  
  12952. if object_id('sysarticles') is not null
  12953.     insert into #queued_tran_objects select distinct objid from sysarticles a,
  12954.         syspublications p where 
  12955.         (objid = @objid or @objid is null) and 
  12956.         a.pubid = p.pubid and
  12957.         p.allow_queued_tran = 1
  12958.  
  12959. select    'table name' = o.name, 
  12960.         'table owner' = user_name(o.uid), 
  12961.         'ID' = o.id, 
  12962.         'Category' = o.category,
  12963.         'HasGuidColumn' = case when 
  12964.                 exists (select * from syscolumns c where c.id = o.id and
  12965.                 xtype = (select xtype from systypes where name = 'uniqueidentifier')) 
  12966.             then cast(1 as bit) 
  12967.             else cast(0 as bit)
  12968.             end,
  12969.         'HasTimeStampColumn' = ObjectProperty(o.id, 'TableHasTimestamp'),
  12970.         'HasRowVersionColumn' = case when 
  12971.                 exists (select * from syscolumns c where c.id = o.id and 
  12972.                 name = N'msrepl_tran_version')
  12973.             then cast(1 as bit) 
  12974.             else cast(0 as bit)
  12975.             end,
  12976.         'HasIdentityColumn' = ObjectProperty(o.id, 'TableHasIdentity'),
  12977.         'HasSQLVariantColumn' = case when 
  12978.                 exists (select * from syscolumns c where c.id = o.id and
  12979.                 xtype = (select xtype from systypes where name = 'sql_variant')) 
  12980.             then cast(1 as bit) 
  12981.             else cast(0 as bit)
  12982.             end,
  12983.         'HasBigIntColumn' = case when 
  12984.                 exists (select * from syscolumns c where c.id = o.id and
  12985.                 xtype = (select xtype from systypes where name = 'bigint'))
  12986.             then cast(1 as bit) 
  12987.             else cast(0 as bit)
  12988.             end,
  12989.         'HasBigIntIdentityColumn' = case when 
  12990.                 exists (select * from syscolumns c where c.id = o.id and
  12991.                 ColumnProperty(o.id, c.name, 'IsIdentity') = 1 and
  12992.                 xtype = (select xtype from systypes where name = 'bigint'))
  12993.             then cast(1 as bit) 
  12994.             else cast(0 as bit)
  12995.             end,
  12996.         'MergePublished' = case when exists (select * from #merge_objects m where
  12997.             m.objid = o.id)
  12998.             then cast(1 as bit) 
  12999.             else cast(0 as bit)
  13000.             end,
  13001.         'QueuedTranPublished' = case when exists (select * from #queued_tran_objects m where
  13002.             m.objid = o.id)
  13003.             then cast(1 as bit) 
  13004.             else cast(0 as bit)
  13005.             end,
  13006.         'HasIdentityNotForReplColumn' = case when 
  13007.                 exists (select * from syscolumns c where c.id = o.id and 
  13008.                     ColumnProperty(o.id, c.name, 'IsIdNotForRepl') = 1)
  13009.             then cast(1 as bit) 
  13010.             else cast(0 as bit)
  13011.             end
  13012.  
  13013.         from sysobjects o where o.xtype = 'U' and 
  13014.             ObjectProperty(o.id, 'IsMSShipped') = 0 and
  13015.             (o.id = @objid or @objid is null)
  13016.             order by 1, 2
  13017.             
  13018. drop table #merge_objects
  13019. drop table #queued_tran_objects
  13020. go
  13021. exec sp_MS_marksystemobject sp_MShelp_replication_table
  13022. grant execute on dbo.sp_MShelp_replication_table to public
  13023. go
  13024. create proc sp_MScopyscriptfile (
  13025.     @scriptfile nvarchar(4000), 
  13026.     @cmd nvarchar(4000) OUTPUT
  13027. )
  13028. as
  13029. declare @directory nvarchar(4000)
  13030. declare @filename nvarchar(1024)
  13031. declare @subdirectory nvarchar(1024)
  13032. declare @retcode int
  13033.  
  13034. IF @scriptfile IS NULL
  13035. BEGIN
  13036.     RAISERROR (14043, 16, -1, '@scriptfile')
  13037.     RETURN (1)
  13038. END
  13039. -- Create the directory on distributor to store script.
  13040. exec sp_helpdistributor @directory=@directory OUTPUT
  13041. select @subdirectory = convert(nvarchar(64), GetDate(), 121)
  13042. select @subdirectory = replace(@subdirectory, N'-', N'')
  13043. select @subdirectory = replace(@subdirectory, N' ', N'')
  13044. select @subdirectory = replace(@subdirectory, N':', N'')
  13045. select @subdirectory = replace(@subdirectory, N'.', N'')
  13046. if(right(@directory, 1) = N'\')
  13047.     select @directory = @directory + @subdirectory
  13048. else
  13049.     select @directory = @directory + N'\' + @subdirectory
  13050. select @cmd = N'md "' + fn_escapecmdshellsymbolsremovequotes(@directory) collate database_default + '"'
  13051. exec @retcode = master..xp_cmdshell @cmd, NO_OUTPUT
  13052. if(@retcode <> 0)
  13053. begin
  13054.     raiserror(21330, 16, -1, @cmd)
  13055.     return (1)
  13056. end
  13057.  
  13058. -- Copy script to distributor
  13059. select @cmd = N'copy "' + fn_escapecmdshellsymbolsremovequotes(@scriptfile) collate database_default + N'" "' + fn_escapecmdshellsymbolsremovequotes(@directory) collate database_default + N'"'
  13060. exec @retcode = master..xp_cmdshell @cmd, NO_OUTPUT
  13061. if(@retcode <> 0)
  13062. begin 
  13063.     raiserror(21331, 16, -1, @cmd)
  13064.     return (1)
  13065. end
  13066.  
  13067. -- Prepare command, to be posted to log
  13068. select @filename = right(@scriptfile, charindex(N'\', reverse(@scriptfile)))
  13069. if(left(@filename, 1) = N'\')    
  13070.     select @cmd = @directory + @filename
  13071. else
  13072.     select @cmd = @directory + N'\' + @filename
  13073. go
  13074. exec sp_MS_marksystemobject sp_MScopyscriptfile
  13075. go
  13076. --
  13077. -- Name:    
  13078. --          sp_replsetoriginator
  13079. --          
  13080. -- Description: 
  13081. --          wrapper for sp_replsetoriginator_internal with DBO check
  13082. --  
  13083. -- Security: 
  13084. --          DBO only
  13085. --
  13086. -- Returns:      0 : success
  13087. --        1 : failure          
  13088. --      
  13089. -- Owner:   
  13090. --          qunguo
  13091. --
  13092. --
  13093. raiserror('Creating procedure sp_replsetoriginator',0,1)
  13094. go
  13095. create proc sp_replsetoriginator (
  13096.     @originator_srv sysname, 
  13097.     @originator_db sysname
  13098. )
  13099. as
  13100.     declare @retcode int
  13101.     /*
  13102.         ** Security Check, this proc is only called by distrib.exe, against sub db as DBO
  13103.         */
  13104.         exec @retcode = dbo.sp_MSreplcheck_subscribe
  13105.         if @@ERROR <> 0 or @retcode <> 0
  13106.     begin
  13107.             return(1)
  13108.     end
  13109.  
  13110.         exec @retcode = dbo.sp_replsetoriginator_internal @originator_srv, @originator_db
  13111.         if @@ERROR <> 0 or @retcode <> 0
  13112.     begin
  13113.             return(1)
  13114.     end
  13115.  
  13116.     return 0
  13117. go
  13118. exec sp_MS_marksystemobject sp_replsetoriginator
  13119. go
  13120. --
  13121. -- Name:    
  13122. --          sp_replsetoriginator_pal
  13123. --          
  13124. -- Description: 
  13125. --          wrapper for sp_replsetoriginator_internal with pull access check
  13126. --  
  13127. -- Security: 
  13128. --          DBO or PAL access
  13129. --
  13130. -- Returns:      0 : success
  13131. --        1 : failure          
  13132. --      
  13133. -- Owner:   
  13134. --          qunguo
  13135. --
  13136. --
  13137. raiserror('Creating procedure sp_replsetoriginator_pal',0,1)
  13138. go
  13139. create proc sp_replsetoriginator_pal (
  13140.     @originator_srv sysname, 
  13141.     @originator_db sysname,
  13142.     @publication sysname
  13143. )
  13144. as
  13145.     declare @retcode int
  13146.     /*
  13147.         ** Security Check, this is called by updatable subscriber in sync-tran proc, hence PAL access
  13148.         */
  13149.     exec @retcode = dbo.sp_MSreplcheck_pull @publication = @publication
  13150.     if @@error <> 0 or @retcode <> 0
  13151.     begin
  13152.         return (1)
  13153.     end
  13154.  
  13155.         exec @retcode = dbo.sp_replsetoriginator_internal @originator_srv, @originator_db
  13156.         if @@ERROR <> 0 or @retcode <> 0
  13157.     begin
  13158.             return(1)
  13159.     end
  13160.  
  13161.     return 0
  13162. go
  13163. exec sp_MS_marksystemobject sp_replsetoriginator_pal
  13164. go
  13165. --
  13166. -- Name:    
  13167. --          sp_replincrementlsn
  13168. --          
  13169. -- Description: 
  13170. --          wrapper for sp_replincrementlsn_internal with DBO check
  13171. --  
  13172. -- Security: 
  13173. --          DBO only
  13174. --
  13175. -- Returns:      0 : success
  13176. --        1 : failure          
  13177. --      
  13178. -- Owner:   
  13179. --          qunguo
  13180. --
  13181. --
  13182. raiserror('Creating procedure sp_replincrementlsn',0,1)
  13183. go
  13184. create proc sp_replincrementlsn (
  13185.     @xact_seqno binary(10) OUTPUT
  13186. )
  13187. as
  13188.     declare @retcode int
  13189.     /*
  13190.         ** Security Check, this proc is called by snapshot agent or sp_repladd(drop)column, against pub db as DBO
  13191.         */
  13192.         exec @retcode = dbo.sp_MSreplcheck_publish
  13193.         if @@ERROR <> 0 or @retcode <> 0
  13194.     begin
  13195.             return(1)
  13196.     end
  13197.  
  13198.         exec @retcode = dbo.sp_replincrementlsn_internal @xact_seqno OUTPUT
  13199.         if @@ERROR <> 0 or @retcode <> 0
  13200.     begin
  13201.             return(1)
  13202.     end
  13203.  
  13204.     return 0
  13205. go
  13206. exec sp_MS_marksystemobject sp_replincrementlsn
  13207. go
  13208. --
  13209. -- Name:    
  13210. --          sp_replpostsyncstatus
  13211. --          
  13212. -- Description: 
  13213. --          wrapper for sp_replpostsyncstatus_int with DBO check
  13214. --  
  13215. -- Security: 
  13216. --          DBO only
  13217. --
  13218. -- Returns:      0 : success
  13219. --        1 : failure          
  13220. --      
  13221. -- Owner:   
  13222. --          qunguo
  13223. --
  13224. --
  13225. raiserror('Creating procedure sp_replpostsyncstatus',0,1)
  13226. go
  13227. create proc sp_replpostsyncstatus (
  13228.     @pubid int, 
  13229.     @artid int, 
  13230.     @syncstat int, 
  13231.     @xact_seqno binary(10) OUTPUT
  13232. )
  13233. as
  13234.     declare @retcode int
  13235.     /*
  13236.         ** Security Check, this proc is called by snapshot agent as DBO on publisher
  13237.         */
  13238.         exec @retcode = dbo.sp_MSreplcheck_publish
  13239.         if @@ERROR <> 0 or @retcode <> 0
  13240.     begin
  13241.             return(1)
  13242.     end
  13243.  
  13244.         exec @retcode = dbo.sp_replpostsyncstatus_int @pubid, @artid, @syncstat, @xact_seqno OUTPUT
  13245.         if @@ERROR <> 0 or @retcode <> 0
  13246.     begin
  13247.             return(1)
  13248.     end
  13249.  
  13250.     return 0
  13251. go
  13252. exec sp_MS_marksystemobject sp_replpostsyncstatus
  13253. go
  13254. /*
  13255. ** Add extended stored procedures for replication support.
  13256. */
  13257. sp_addextendedproc  'sp_repldone', 'repldone extended procedure'
  13258. go
  13259.  
  13260. sp_addextendedproc  'sp_repltrans', 'repltrans extended procedure'
  13261. go
  13262.  
  13263. sp_addextendedproc  'sp_replcounters', 'replcounters extended procedure'
  13264. go
  13265.  
  13266. sp_addextendedproc  'sp_replcmds', 'replcmds extended procedure'
  13267. go
  13268.  
  13269. sp_addextendedproc  'sp_replflush', 'replflush extended procedure'
  13270. go
  13271.  
  13272. dump tran master with no_log
  13273. go
  13274.  
  13275. sp_addextendedproc  'sp_replpostcmd', 'replpostcmd extended procedure'
  13276. go
  13277. sp_addextendedproc  'sp_replpostschema', 'replpostschema extended procedure'
  13278. go
  13279.  
  13280. sp_addextendedproc  'sp_replincrementlsn_internal', 'replincrementlsn extended procedure'
  13281. go
  13282.  
  13283. sp_addextendedproc  'sp_replupdateschema', 'replupdateschema extended procedure'
  13284. go
  13285.  
  13286. sp_addextendedproc  'sp_replsetoriginator_internal', 'replsetoriginator extended procedure'
  13287. go
  13288.  
  13289. sp_addextendedproc  'sp_replsetsyncstatus', 'replsetsyncstatus extended procedure'
  13290. go
  13291.  
  13292. sp_addextendedproc  'sp_replpostsyncstatus_int', 'replpostsyncstatus_int extended procedure'
  13293. go
  13294.  
  13295. /*
  13296. ** Add xp_enum_dsn extended procedure
  13297. */
  13298. sp_addextendedproc 'xp_dsninfo','xpstar.dll'
  13299. go
  13300. exec dbo.sp_MS_marksystemobject xp_dsninfo
  13301. go
  13302.  
  13303. /*
  13304. ** Add xp_enum_dsn extended procedure
  13305. */
  13306. sp_addextendedproc 'xp_enumdsn','xpstar.dll'
  13307. go
  13308. exec dbo.sp_MS_marksystemobject xp_enumdsn
  13309. go
  13310.  
  13311. /*
  13312. ** Add xp_oledbinfo extended procedure
  13313. */
  13314. sp_addextendedproc 'xp_oledbinfo','xprepl.dll'
  13315. go
  13316. exec dbo.sp_MS_marksystemobject xp_oledbinfo
  13317. go
  13318.  
  13319. /*
  13320. ** Add xp_repl_encrypt extended procedure
  13321. */
  13322. sp_addextendedproc 'xp_repl_encrypt','xprepl.dll'
  13323. go
  13324. exec dbo.sp_MS_marksystemobject xp_repl_encrypt
  13325. go
  13326.  
  13327.  
  13328. /*
  13329. ** Add xp_repl_convert_encrypt extended procedure
  13330. */
  13331. sp_addextendedproc 'xp_repl_convert_encrypt','xprepl.dll'
  13332. go
  13333. exec dbo.sp_MS_marksystemobject xp_repl_convert_encrypt
  13334. go
  13335.  
  13336.  
  13337.  
  13338. /*
  13339. ** Add xp_repl_convert_encrypt extended procedure
  13340. */
  13341. sp_addextendedproc 'xp_repl_help_connect','xprepl.dll'
  13342. go
  13343. exec dbo.sp_MS_marksystemobject xp_repl_help_connect
  13344. go
  13345.  
  13346.  
  13347.  
  13348. /*
  13349. ** Add xp_replproberemsrv extended procedure
  13350. */
  13351. sp_addextendedproc 'xp_replproberemsrv','xprepl.dll'
  13352. go
  13353. exec dbo.sp_MS_marksystemobject xp_replproberemsrv
  13354. go
  13355.  
  13356. dump tran master with no_log
  13357. go
  13358.  
  13359. grant execute on dbo.sp_addpublication_snapshot to public
  13360. go
  13361. grant execute on dbo.sp_MShelpobjectpublications to public
  13362. go
  13363. grant execute on dbo.sp_helpreplicationdb to public
  13364. go
  13365. grant execute on dbo.sp_enumdsn to public
  13366. go
  13367. grant execute on dbo.sp_helpsubscriberinfo to public
  13368. go
  13369. grant execute on dbo.sp_replica to public
  13370. go
  13371. grant execute on dbo.sp_distcounters to public
  13372. go
  13373. grant execute on dbo.sp_helpdistributor to public
  13374. go
  13375. grant execute on dbo.sp_helpdistributiondb to public
  13376. go
  13377. grant execute on dbo.sp_helpdistpublisher to public
  13378. go
  13379. grant execute on dbo.sp_replcounters to public
  13380. go
  13381. grant execute on dbo.sp_MShelp_distdb to public
  13382. go
  13383. grant execute on dbo.sp_grant_publication_access to public
  13384. go
  13385. grant execute on dbo.sp_revoke_publication_access to public
  13386. go
  13387. grant execute on dbo.sp_help_publication_access to public
  13388. go
  13389. grant execute on dbo.sp_check_publication_access to public
  13390. go
  13391. grant execute on dbo.sp_replsetoriginator to public
  13392. go
  13393. grant execute on dbo.sp_replsetoriginator_pal to public
  13394. go
  13395.  
  13396.  
  13397. GRANT EXECUTE ON dbo.sp_MShelpconflictpublications TO PUBLIC
  13398. GO
  13399.  
  13400. grant execute on dbo.sp_repldone to public
  13401. go
  13402. grant execute on dbo.sp_repltrans to public
  13403. go
  13404. grant execute on dbo.sp_replcounters to public
  13405. go
  13406. grant execute on dbo.sp_replcmds to public
  13407. go
  13408. grant execute on dbo.sp_replpostschema to public
  13409. go
  13410. grant execute on dbo.sp_replincrementlsn to public
  13411. go
  13412. grant execute on dbo.sp_replsetsyncstatus to public
  13413. go
  13414. grant execute on dbo.sp_replpostsyncstatus to public
  13415. go
  13416. grant execute on sp_MSenum_replication_job to public
  13417. go
  13418. grant execute on sp_MSrepl_gettype_mappings to public
  13419. go
  13420. grant execute on sp_MShelp_replication_status to public
  13421. grant execute on sp_MSenum_replication_agents to public
  13422. -- proc need to be granted to public for replmonitor role
  13423. grant execute on sp_help_agent_default to public
  13424. grant execute on sp_help_agent_profile to public
  13425. grant execute on sp_help_agent_parameter to public
  13426. grant execute on sp_helpdistributor_properties to public
  13427. grant execute on sp_MScleanupdynsnapshotvws to public
  13428. grant execute on sp_MSget_agent_names to public
  13429. go
  13430.  
  13431.  
  13432. dump tran master with no_log
  13433. go
  13434.  
  13435.  
  13436.  
  13437. dump tran master with no_log
  13438. go
  13439. sp_configure 'allow updates',0
  13440. go
  13441. reconfigure with override
  13442. go
  13443.  
  13444. print ''
  13445. print 'Checking objects created by replcom.sql.'
  13446. go
  13447. --obsolete   exec dbo.sp_check_objects 'repl'
  13448. exec dbo.sp_MS_upd_sysobj_category 2  --set sysobjects.category | 2 based on crdate.
  13449. go
  13450.  
  13451. print ''
  13452. print 'replcom.sql completed successfully.'
  13453. go
  13454.  
  13455. dump tran master with no_log
  13456. go
  13457. checkpoint
  13458. go
  13459. -- - -----
  13460.